繁体   English   中英

OpenId Connect和自定义身份框架

[英]OpenId Connect and Custom Identity Framework

我使用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;
                  }
                }
        });

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

暂无
暂无

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

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