使用 Microsoft.Identity.Web 的 Azure AD 登录在不同的应用程序中返回不同的声明名称

问题描述 投票:0回答:0

我有 2 个 Umbraco Web 应用程序在 .NET Core 6 上运行,它们都配置为使用相同的 Azure AD 应用程序注册进行后台身份验证基于此处的代码.

作为令牌验证的一部分,我需要确保我们有用户的电子邮件地址,为此我们使用以下代码:

public static Task OnTokenValidated(TokenValidatedContext context) {
  var cp = context.Principal;

  if (cp != null)
  {
    // we want to keep name, subject and roles
    var email = cp.FindFirst(ClaimTypes.Email) ?? cp.FindFirst(ClaimTypes.Upn);

    if (email != null &&
        cp.FindFirst(ClaimTypes.Email) == null &&
        context.Principal?.Identity is ClaimsIdentity claimsIdentity)
    {
      claimsIdentity.AddClaim(new Claim(ClaimTypes.Email, email.Value));
    }
  }
}

使用

System.Security.Claims
中的标准声明名称,例如
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
.

在一个应用程序上,这工作正常,并且声明正确通过:

在另一个应用程序中,声明以

email
roles
的形式返回,因此没有被拾取,并且该应用程序也没有陷入
OnExternalLogin

我还注意到,根据正在运行的请求,当调用 auth 提供程序时,会传入不同的参数:

工作:

https://login.windows.net/.../oauth2/authorize?client_id=...
    &redirect_uri=https://localhost:44323/umbraco-aad-signin
    &response_type=id_token
    &scope=openid profile
    &response_mode=form_post
    &nonce=638179674819211584...
    &state=CfD...
    &x-client-SKU=ID_NETSTANDARD2_0
    &x-client-ver=6.16.0.0

虽然失败的版本有 .net 6 SKU 和 6.25 的客户端版本

https://login.windows.net/.../oauth2/authorize?client_id=...
    &redirect_uri=https://localhost:44373/umbraco-aad-signin
    &response_type=id_token
    &scope=openid profile
    &response_mode=form_post
    &nonce=638179675514533847...
    &state=CfD...
    &x-client-SKU=ID_NET6_0
    &x-client-ver=6.25.1.0

两个项目都使用相同版本的 Microsoft.Identity.Web (1.23.1),我正在努力寻找应用程序的配置或设置方面的任何显着差异。

c# azure azure-active-directory claims-based-identity microsoft.identity.web
© www.soinside.com 2019 - 2024. All rights reserved.