OpenId Connect 和自定义身份框架
OpenId Connect and Custom Identity Framework
我正在使用 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 令牌本身并不会使您登录到应用程序。在采取登录或拒绝操作之前,应用需要根据令牌内容做一些更多的工作。
我正在使用 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 令牌本身并不会使您登录到应用程序。在采取登录或拒绝操作之前,应用需要根据令牌内容做一些更多的工作。