目标: 我想用 OpenID Connect 保护我的 Web 服务(使用 OWIN 的 ASP.NET Web API)。身份提供者是 Google。
重要提示: 我只负责网络服务。我没有任何 Web 应用程序(没有 UI;这取决于使用我的 Web 服务的第 3 方)。
注意事项: 由于我的 Web 服务在服务器上运行,我认为最好使用授权代码流(或混合?)。
到目前为止所做的事情:我将 OWIN(在我的 Web 服务中)配置为使用 OpenID Connect ( UseOpenIdConnectAuthentication
)。
当前行为: 我有人未经授权尝试访问网络服务,网络服务将她(发送 302)重定向到谷歌。由于我没有 UI,因此需要由第 3 方正确显示同意页面。假设用户同意并按下“接受”。
我在这里卡住了......据我了解,谷歌发回了一个授权码(我的网络服务可以用来获取访问令牌)。如何获得授权码?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);
}
},
});
我是否必须手动调用 Google(以获取访问令牌)然后登录(使用GetOwinContext().Authentication.SignIn(id);
)或者 OWIN 中间件会处理这个问题?
更新:这里有一个repro:https ://github.com/Dunken/WebApiOpenIdConnect