【问题标题】:OpenId Connect and Custom Identity FrameworkOpenId Connect 和自定义身份框架
【发布时间】:2019-06-24 06:38:26
【问题描述】:

我正在使用 Okta 示例在 Asp.NET 4.6.x MVC Web 应用程序中实现 OpenIdConnect。该应用程序使用 Unity 进行依赖注入,其中一个依赖项是 Identity Framework 的一组自定义类。我没有使用 Okta API,因为 IdP 实际上并不是 Okta,我假设其中有专有的东西。所以这都是 OpenId 部分的 .NET 标准库。

单击登录后,我可以浏览代码,它会将我带到 IdP,我可以使用我的帐户登录,然后它会带我回来,我可以看到他们提供的所有信息以进行登录。但它不会像 Okta 的 GitHub 中的示例那样让我登录或进行任何操作。

基本上我想知道身份自定义是否会干扰登录,是否有办法介入其中并指定我需要它做什么?

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions {
            ClientId = clientId 
            , ClientSecret = clientSecret
            , Authority = authority
            , RedirectUri = redirectUri
            , AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive
            , ResponseType = OpenIdConnectResponseType.CodeIdToken
            , Scope = OpenIdConnectScope.OpenIdProfile
            , PostLogoutRedirectUri = postLogoutRedirectUri
            , TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" }
            , Notifications = new OpenIdConnectAuthenticationNotifications {
                AuthorizationCodeReceived = async n =>
                {
                    //var tokenClient = new TokenClient($"{authority}/oauth2/v1/token", clientId, clientSecret);
                    var tokenClient = new TokenClient($"{authority}/connect/token", clientId, clientSecret);
                    var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, redirectUri);

                    if (tokenResponse.IsError)
                    {
                        throw new Exception(tokenResponse.Error);
                    }

                    //var userInfoClient = new UserInfoClient($"{authority}/oauth2/v1/userinfo");
                    var userInfoClient = new UserInfoClient($"{authority}/connect/userinfo");
                    var userInfoResponse = await userInfoClient.GetAsync(tokenResponse.AccessToken);
                    var claims = new List<System.Security.Claims.Claim>();
                    claims.AddRange(userInfoResponse.Claims);
                    claims.Add(new System.Security.Claims.Claim("id_token", tokenResponse.IdentityToken));
                    claims.Add(new System.Security.Claims.Claim("access_token", tokenResponse.AccessToken));

                    if (!string.IsNullOrEmpty(tokenResponse.RefreshToken))
                    {
                        claims.Add(new System.Security.Claims.Claim("refresh_token", tokenResponse.RefreshToken));
                    }

                    n.AuthenticationTicket.Identity.AddClaims(claims);

                    return;
                }
                , RedirectToIdentityProvider = n =>
                  {
                    // If signing out, add the id_token_hint
                    if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
                    {
                          var idTokenClaim = n.OwinContext.Authentication.User.FindFirst("id_token");

                          if (idTokenClaim != null)
                          {
                              n.ProtocolMessage.IdTokenHint = idTokenClaim.Value;
                          }

                    }

                    return Task.CompletedTask;
                  }
                }
        });

【问题讨论】:

    标签: c# asp.net-mvc openid-connect okta


    【解决方案1】:

    Okta 返回的令牌必须由您的应用程序管理才能执行登录操作。返回的 OIDC 令牌需要您进行验证和验证,然后决定是否接受 OIDC 令牌。如果是这样,您将采取措施将用户登录到您的应用程序中。作为 OpenID Connect 流的结果接收 OIDC 令牌本身并不会将您登录到应用程序。该应用需要在执行登录或拒绝操作之前根据令牌内容做更多的工作。

    【讨论】:

    • 事实证明这或多或少是我必须做的。上面的代码确实处理了很多,但在示例应用程序中自动工作的部分已被我的自定义设置所征服。所以我最终编写了一些代码来处理上面代码示例的“AuthorizationCodeReceived”部分中的位。
    猜你喜欢
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2019-03-14
    • 2015-04-29
    • 2021-09-07
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多