【问题标题】:asp.net identity: after authentication, add custom user claims to a token provided by AADasp.net 身份:身份验证后,将自定义用户声明添加到 AAD 提供的令牌
【发布时间】:2018-02-22 08:43:08
【问题描述】:

在 Azure Active Directory 成功验证 web 应用程序后,我需要向令牌添加自定义声明。

基本上我想在我的一个控制器中执行此操作:

if (ExistsUserInDb(User.Identity.Name))
{
   User.Identity.AddClaim("superUser", "true");
}

这样当该用户在其他控制器上执行一些 superPrivilege 操作时,我可以继续重复使用相同的令牌。

这可能吗?

我已经尝试了这些链接,但它们对我不起作用: How to extend available properties of User.Identity How to add claims in ASP.NET Identity

【问题讨论】:

    标签: asp.net asp.net-mvc active-directory asp.net-identity azure-active-directory


    【解决方案1】:

    asp.net 身份:身份验证后,将自定义用户声明添加到 AAD 提供的令牌中

    根据我的理解,您的 MVC 应用程序配置为使用 ASP.NET Identity 进行用户身份验证,并且您还使用 Microsoft.Owin.Security.ActiveDirectory 包来支持 AAD JWT 不记名令牌身份验证,如下所示:

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            TokenValidationParameters = new TokenValidationParameters()
            {
                ValidAudience = "{AAD-client-ID}"
            },
            Tenant = "{tenantID}"
        });
    

    此时,上述中间件将解码令牌并创建一个ClaimsIdentity 用于包装来自传入 JWT 令牌的声明。根据我的理解,您无法修改控制器下的传入令牌,但您可以在中间件设置下处理此问题,如下所示:

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            TokenValidationParameters = new TokenValidationParameters()
            {
                ValidAudience = "{AAD-client-ID}"
            },
            Tenant = "{tenantID}",
            Provider = new OAuthBearerAuthenticationProvider()
            {
                OnValidateIdentity = (context) =>
                {   
                    //check context.Ticket.Identity.Name
                    //add your additional claims here
                    context.Ticket.Identity.AddClaim(new Claim("test02", "test02"));
                    return Task.FromResult(0);
                }
            }
        });
    

    此外,我将使用Microsoft.Owin.Security.OpenIdConnect 中间件来使用 OpenIdConnect 进行 AAD 身份验证,如下所示:

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = Authority,
            PostLogoutRedirectUri = postLogoutRedirectUri,
            Notifications = new OpenIdConnectAuthenticationNotifications()
            {
                SecurityTokenValidated = async (x) =>
                {
                    var identity = x.AuthenticationTicket.Identity;
    
                    //check the name, add additional claims 
                    identity.AddClaim(new Claim("test", "test"));
    
                    await Task.FromResult(0);
                }
            }
        });
    

    或者您可以尝试在控制器中添加声明,如下所示:

    var identity= User.Identity as ClaimsIdentity;
    identity.AddClaim(new Claim("test1", "test1"));
    HttpContext.GetOwinContext().Authentication.SignIn(identity);
    

    详情,您可以关注Integrate Azure AD into a web application using OpenID Connect

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2014-09-28
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多