使用Google OpenID Connect的ASP.NET Web API

Dun*_*ken 9 oauth-2.0 asp.net-web-api google-oauth owin openid-connect

目标: 我想使用OpenID Connect保护我的Web服务(使用OWIN的ASP.NET Web API).身份提供商是Google.

重要提示: 我只负责Web服务.我没有任何Web应用程序(没有UI;这取决于使用我的Web服务的第三方).

注意事项: 由于我的Web服务在服务器上运行,我认为最好使用授权代码流(或混合?).

到目前为止做了什么:我配置OWIN(在我的Web服务中)使用OpenID Connect(UseOpenIdConnectAuthentication).

当前行为: 我有人未经授权尝试访问Web服务,Web服务将她重定向(发送302)给Google.由于我没有UI,因此需要第三方正确显示同意页面.假设用户同意并按"接受".

在这里,我被卡住了...据我所知,谷歌发回了授权码(我的网络服务可以使用它来获取访问令牌).我怎样才能获得授权码?Google甚至支持授权代码流程(ResponseType = code)吗?

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    ClientId = "aClientId",
    ClientSecret = "aClientSecret",
    Authority = "https://accounts.google.com/",
    RedirectUri = "do I need this??",  //I guess here I would register a resources... but what to do in there?
    ResponseType =  "code",            //does Google even support code??
    Scope = "openid",                
    Notifications = new OpenIdConnectAuthenticationNotifications()
    {
        AuthorizationCodeReceived = (context) =>
        {
            var code = context.Code; //I never got to this point so far
            //do stuff
            return Task.FromResult(0);
        }
    },
});
Run Code Online (Sandbox Code Playgroud)

我是否必须手动调用Google(获取访问令牌)然后使用SignIn(使用GetOwinContext().Authentication.SignIn(id);)或者OWIN中间件是否需要处理此问题?

更新:一个repro在这里:https://github.com/Dunken/WebApiOpenIdConnect