asp.net Micorosf Entra IDX10503:签名验证失败,托肯没有孩子Keys尝试:'System.Text.StringBuilder'

hk8txs48  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(573)

我正在尝试使用Owin通过ASP NET应用程序实现Azure AD身份验证。
我使用的代码如下:

  1. app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  2. app.UseCookieAuthentication(new CookieAuthenticationOptions());
  3. app.UseOpenIdConnectAuthentication(
  4. new OpenIdConnectAuthenticationOptions
  5. {
  6. ClientId = clientId,
  7. Authority = authority,
  8. RedirectUri = redirectUri,
  9. PostLogoutRedirectUri = redirectUri,
  10. Scope = OpenIdConnectScope.OpenId,
  11. ResponseType = OpenIdConnectResponseType.CodeIdToken,
  12. Notifications = new OpenIdConnectAuthenticationNotifications
  13. {
  14. AuthenticationFailed = OnAuthenticationFailed,
  15. AuthorizationCodeReceived = OnAuthorizationCodeReceived,
  16. }
  17. }
  18. );

我得到以下错误:IDX10503: Signature validation failed. Token does not have a kid. Keys tried: 'System.Text.StringBuilder'
我检查了代币,孩子在里面。根据:https://learn.microsoft.com/en-us/azure/active-directory/develop/signing-key-rollover,NET OWIN OpenID Connect已经具有自动处理密钥翻转的必要逻辑。

fdbelqdn

fdbelqdn1#

错误消息IDX10503: Signature validation failed. Token does not have a kid. Keys tried: 'System.Text.StringBuilder'表示正在验证的JSON Web Token(JWT)缺少kid(密钥标识符)声明,这对于标识用于签名JWT的密钥至关重要。
要解决这个问题,可以使用OpenIdConnectAuthenticationOptions类,它的特点是在令牌验证后触发SecurityTokenValidated事件。在事件处理程序中,您可以使用其他声明(如ClaimTypes.Name声明)来增强标识。

  1. app.UseOpenIdConnectAuthentication(
  2. new OpenIdConnectAuthenticationOptions
  3. {
  4. ClientId = clientId,
  5. Authority = authority,
  6. PostLogoutRedirectUri = postLogoutRedirectUri,
  7. Notifications = new OpenIdConnectAuthenticationNotifications()
  8. {
  9. SecurityTokenValidated = (context) =>
  10. {
  11. string name = context.AuthenticationTicket.Identity.FindFirst("preferred_username").Value;
  12. context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Name, name, string.Empty));
  13. return System.Threading.Tasks.Task.FromResult(0);
  14. }
  15. }
  16. });
  17. }

请确保您已安装以下软件包

  1. Install-Package Microsoft.Owin
  2. Install-Package Microsoft.Owin.Security.OpenIdConnect
  3. Install-Package Microsoft.Owin.Security.Cookies
  4. Install-Package Microsoft.Owin.Host.SystemWeb
  5. Install-Package Microsoft.IdentityModel.Protocol.Extensions
  6. Install-Package System.IdentityModel.Tokens.Jwt

结果

展开查看全部

相关问题