【问题标题】:ADAL bearer token does not return username but aad client idADAL 不记名令牌不返回用户名,而是返回 aad 客户端 ID
【发布时间】:2017-03-17 14:13:16
【问题描述】:

我们有一个桌面应用程序,它使用 adal 对用户进行身份验证,使用以下代码:

AuthenticationResult result = null;
var context = new  AuthenticationContext(aadTenantDomain);
result = await context.AcquireTokenAsync(resourceId, clientId, returnUrl, new PlatformParameters(PromptBehavior.Auto));

这工作正常,返回的 AuthenticationResult 包含所有正确的用户信息。现在我们使用从 AuthenticationResult 获得的访问令牌调用托管在 azure 上的 web 应用程序 web api 控制器:

var Client = new HttpClient();
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);

这也正确地授权了用户。现在在 Web 控制器中,我们使用 User.Identity.Name 来获取由访问令牌授权的用户名。到昨天为止的许多个月里,这运作良好,但今天 User.Identity.Name 返回桌面应用程序的客户端 ID,而不是用户名。有谁知道可能出了什么问题?

这是 api 认证配置:

 public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {                   
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,                     
                TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {
                    RoleClaimType = System.Security.Claims.ClaimTypes.Role,                         
                }
            });
    }

这是一个示例控制器功能:

[Authorize]
public class dialplanController : ApiController
{
    public async Task<IHttpActionResult> GetMe()
    {
        var Me = db.dialplan.FirstOrDefault(d => d.email == User.Identity.Name);
        return Ok(Me);
    }
}

【问题讨论】:

  • 你检查过token是否包含用户名吗?
  • @juunas 我刚刚检查过,upn 字段和 unique_name 字段都包含用户名。
  • 能否将 API 中的身份验证代码添加到问题中?
  • @juunas 我添加了 api auth 配置代码和一个示例控制器,其中返回客户端 api id 而不是用户名。
  • 嗯,但您不应该在 API 端进行 JWT Bearer 令牌身份验证吗? OIDC 用于 Web 应用程序中基于重定向的身份验证。

标签: c# .net azure model-view-controller adal


【解决方案1】:

如果您希望 Web 应用程序同时支持 OpenIdConnection 和 Windows Azure Active Directory 不记名令牌,则需要添加代码 app.UseWindowsAzureActiveDirectoryBearerAuthentication(),如 juunas 所述。

例如,以下是供您参考的代码:

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions());

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = Authority,
            PostLogoutRedirectUri = redirectUri,
            RedirectUri = redirectUri,
            Notifications = new OpenIdConnectAuthenticationNotifications()
            {
                //
                // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                //
                AuthorizationCodeReceived = OnAuthorizationCodeReceived,
                AuthenticationFailed = OnAuthenticationFailed,
                RedirectToIdentityProvider= OnRedirectToIdentityProvider,
                MessageReceived= OnMessageReceived,
                SecurityTokenReceived= OnSecurityTokenReceived,
            },
        });

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(new Microsoft.Owin.Security.ActiveDirectory.WindowsAzureActiveDirectoryBearerAuthenticationOptions
    {
        Audience = "",
        Tenant = "",
    });
}

之后,我们可以使用从 Azure AD 获取的令牌调用 Web API。 Azure Active Directory OWIN 组件将根据 access_token 中的 unique_name 声明将 User.Identity.Name 从委托令牌转换。

请解码来自this site 的访问令牌以查看是否需要unique_name

【讨论】:

    【解决方案2】:

    使用 ClaimsPrincipal.Current?.FindFirst(ClaimTypes.Upn)?.Value 返回当前用户的正确 UPN,如果没有给出 upn,则返回 null。

    【讨论】:

      猜你喜欢
      • 2021-05-07
      • 2014-07-28
      • 1970-01-01
      • 2019-08-08
      • 2016-04-07
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多