繁体   English   中英

用于 OpenID Connect 的 OWIN 中间件 - 代码流(流类型 - AuthorizationCode)文档?

[英]OWIN middleware for OpenID Connect - Code flow ( Flow type - AuthorizationCode) documentation?

在我的实现中,我使用 OpenID-Connect Server (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/Roadmap#410- 2019 年 11 月发布


OpenID Connect 中间件不支持代码流: 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

Pinpoint 的回答和评论回复很到位。 谢谢!

但是,如果您愿意离开 NuGet 包,转而为 Microsoft.Owin.Security.OpenIdConnect 运行修改后的源代码,则可以使用 form_post 获取代码 ( code ) 流。

当然,对于所有开源项目问题都可以这样说,但在我的情况下,这是一个大问题的快速解决方案,所以我想我会分享它可能是一个选择。

我从https://github.com/aspnet/AspNetKatana下载了代码,将 csproj 添加到我的解决方案中,并从https://github.com/aspnet/AspNetKatana/blob/dev/src/Microsoft.Owin.Security 中删除了行 AuthenticateCoreAsync() 中的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;
    }
    ..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM