我正在使用Okta示例在 Asp.NET 4.6.x MVC Web应用程序中实现OpenIdConnect。 应用程序使用 Unity 进行依赖项注入,其中一个依赖项是标识框架的一组自定义类。 我没有使用 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 令牌本身不会将您登录到应用程序。在执行登录或拒绝操作之前,应用需要根据令牌内容执行更多工作。