【问题标题】:OWIN middleware for OpenID Connect - Code flow ( Flow type - AuthorizationCode) documentation?OpenID Connect 的 OWIN 中间件 - 代码流(流类型 - AuthorizationCode)文档?
【发布时间】:2016-02-13 05:11:16
【问题描述】:

在我的实现中,我使用 OpenID-Connect Server (Identity Server v3+) 来验证 Asp.net MVC 5 应用程序(带有 AngularJS 前端)

我计划使用 OID 代码流(使用 Scope Open_ID)对客户端 (RP) 进行身份验证。对于 OpenID 连接中间件,我使用的是 OWIN(Katana 项目)组件。

在实施之前,我想了解使用 OWIN 的反向通道令牌请求、刷新令牌请求过程等。但是我无法找到此类实现的任何文档(大多数可用示例使用隐式流) .

我可以在这里https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source找到 ID Server v3 的通用代码流实现示例

我正在寻找使用 OWIN 中间件的类似产品?有没有人有任何指示?

【问题讨论】:

    标签: asp.net-mvc oauth-2.0 owin openid-connect identityserver3


    【解决方案1】:

    编辑:好消息,代码流和response_mode=query 支持终于添加到了 Katana,作为 4.1 版本(于 2019 年 11 月发布)的一部分:https://github.com/aspnet/AspNetKatana/wiki/Roadmap#410-release-november-2019


    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 的示例。

    【讨论】:

    • 这个答案包含了很多知识。您一定花了一些时间来发现所有这些痛点。扩展 OP 的 回调期间抛出的异常:如果您只要求 code 流(当然是由设计)。
    • @CrescentFresh 谢谢你的好话!实际上,我已经为 OIDC 中间件贡献了几次(例如,我引入了 response_mode=query 支持)并为 OWIN/Katana 和 ASP.NET 5 (github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server) 开发了服务器对应项,这就解释了为什么我对与 OIDC 相关的问题感到满意;)我更新了我的答案以纳入您的精确度,谢谢!
    • @JohanKronberg Katana 4.0 即将发布,但代码流支持方面没有任何变化。随意在 GitHub (github.com/aspnet/AspNetKatana) 上开一张新票,但我不确定它是否会在不久的将来得到支持。
    • 已为 4.1 github.com/aspnet/AspNetKatana/pull/297添加了代码流支持
    • 谁能给我一个配置 Owin 4.1 以使用身份验证代码流的示例?我发现的所有样本似乎都使用了隐式流。
    【解决方案2】:

    Pinpoint 的答案和评论回复很准确。谢谢!

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

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

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

    【讨论】:

    猜你喜欢
    • 2018-05-20
    • 2015-12-11
    • 2017-06-28
    • 2018-06-19
    • 2020-03-29
    • 2017-06-05
    • 2019-10-24
    • 1970-01-01
    • 2017-04-18
    相关资源
    最近更新 更多