在我的实现中,我使用 OpenID-Connect 服务器(Identity Server v3+)来验证 Asp.net MVC 5 应用程序(使用 AngularJS 前端)
我计划使用 OID 代码流(带有 Scope Open_ID)来验证客户端 (RP)。对于 OpenID 连接中间件,我使用 OWIN(Katana 项目)组件。
在实现之前,我想使用 OWIN 了解反向通道令牌请求、刷新令牌请求流程等。但是我无法找到此类实现的任何文档(大多数可用示例都使用隐式流程)。
我可以在这里找到 ID Server v3 的通用代码流实现示例 https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source
我正在寻找使用 OWIN 中间件的类似产品?有人有任何指点吗?
编辑:好消息,代码流程和
response_mode=query
支持终于添加到了 Katana,作为 4.1 版本(于 2019 年 11 月发布)的一部分:https://github.com/aspnet/AspNetKatana/wiki/路线图#410-release-2019 年 11 月.
如果您希望它处理与令牌端点的通信,请务必将 RedeemCode
属性设置为 true。
http://katanaproject.codeplex.com/workitem/247(不过,它已在 ASP.NET 5 版本中修复)。
实际上,官方仅支持隐式流(id_token
),您必须使用
response_mode=form_post
扩展。尝试使用授权代码流只会导致在回调期间引发异常,因为它将无法从身份验证响应中提取(丢失的)
id_token
。虽然不直接支持,但您也可以使用混合流(
code
+
id_token (+ token)
),但由您来实现令牌请求部分。您可以参阅https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/dev/samples/Nancy/Nancy.Client/Startup.cs#L82-L115 作为示例。
但是,如果您愿意放弃 NuGet 包,转而运行 Microsoft.Owin.Security.OpenIdConnect 的修改后的源代码,您可以使用 form_post 获取代码 (
code
) 流。当然,这可以适用于所有开源项目问题,但对于我来说,这是一个针对重大问题的快速解决方案,所以我想我会分享它
可能是一个选项。
我从https://github.com/aspnet/AspNetKatana 下载了代码,将 csproj 添加到我的解决方案中,并从 https://github.com/aspnet/AspNetKatana/blob/dev/src/Microsoft 中删除了行。 AuthenticateCoreAsync() 中的 Owin.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs。
然后,您必须将其与反向渠道调用结合起来,然后创建您自己的新 ClaimsIdentity() 以设置为 notification.AuthenticationTicket。
// Install-Package IdentityModel to handle the backchannel calls in a nicer fashion
AuthorizationCodeReceived = async notification =>
{
var configuration = await notification.Options.ConfigurationManager
.GetConfigurationAsync(notification.Request.CallCancelled);
var tokenClient = new TokenClient(configuration.TokenEndpoint,
notification.Options.ClientId, notification.Options.ClientSecret,
AuthenticationStyle.PostValues);
var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(
notification.ProtocolMessage.Code,
"http://localhost:53004/signin-oidc",
cancellationToken: notification.Request.CallCancelled);
if (tokenResponse.IsError
|| string.IsNullOrWhiteSpace(tokenResponse.AccessToken)
|| string.IsNullOrWhiteSpace(tokenResponse.RefreshToken))
{
notification.HandleResponse();
notification.Response.Write("Error retrieving tokens.");
return;
}
var userInfoClient = new UserInfoClient(configuration.UserInfoEndpoint);
var userInfoResponse = await userInfoClient.GetAsync(tokenResponse.AccessToken);
if (userInfoResponse.IsError)
{
notification.HandleResponse();
notification.Response.Write("Error retrieving user info.");
return;
}
..