Azure AAD基于角色的身份验证,User.IsInRole()

时间:2016-12-15 10:47:33

标签: asp.net-mvc azure azure-web-sites azure-active-directory

我正在使用基于Azure AD角色的身份验证,我添加了2个角色(Observer,Reader),这些角色分配给特定用户,工作正常。清单文件中包含这两个新条目。所有身份验证都正常工作,但我无法让User.IsInRole()返回true,总是返回false

我已将以下代码添加到Startup.cs

app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = SettingsHelper.ClientId,
                    Authority = String.Format(CultureInfo.InvariantCulture,SettingsHelper.AADInstance, SettingsHelper.TenantId), 
                                                                                                                                                  PostLogoutRedirectUri = SettingsHelper.PostLogoutRedirectUri,

                    TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                    {
                        // map the claimsPrincipal's roles to the roles claim
                        RoleClaimType = "roles",
                    },
}

当我查询User.IsInRole(" Observer")时,它返回false。我可以在用户

的调试会话中看到声明

{http://schemas.microsoft.com/ws/2008/06/identity/claims/role:观察员}

但是无法访问它,这是一个常见的问题,我做错了吗?

这是我的USER会话var

enter image description here 感谢

3 个答案:

答案 0 :(得分:3)

  

{http://schemas.microsoft.com/ws/2008/06/identity/claims/role:观察员}

您是如何添加自定义角色的?以下是为我添加自定义角色以供参考的步骤:

  1. 在Azure AD上注册应用
  2. 修改其清单以添加自定义角色,如下所示:
  3. "appRoles": [
        {
          "allowedMemberTypes": [
            "User"
          ],
          "displayName": "Orders",
          "id": "51e10148-16a8-432a-b86d-ef620c3e48ed",
          "isEnabled": true,
          "description": "Oders can rise a order request",
          "value": "Orders"
        },
        {
          "allowedMemberTypes": [
            "User"
          ],
          "displayName": "Admin",
          "id": "51e10148-16a8-432a-b86d-ef620c3e48ec",
          "isEnabled": true,
          "description": "Admins can manage roles and perform all task actions.",
          "value": "Admin"
        }
      ],
    
    1. 通过门户网站将角色分配给用户
    2. 使用您的orignal帖子中的代码将Web应用程序与Azure集成
    3.   app.UseOpenIdConnectAuthentication(
                      new OpenIdConnectAuthenticationOptions
                      {
                          ClientId = clientId,
                          Authority = authority,
                          PostLogoutRedirectUri = postLogoutRedirectUri,
                          RedirectUri = postLogoutRedirectUri,
                          TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                          {
                              // map the claimsPrincipal's roles to the roles claim
                              RoleClaimType = "roles",
                          },
                      });
      

      然后我们可以获得如下图所示的角色:

      enter image description here

答案 1 :(得分:2)

我遇到了完全相同的问题,对我来说,答案是@juunas在他对你原来问题的评论中所说的话:Azure AAD Role Based Authentication, User.IsInRole()

从字面上改变你的Startup.Auth.cs中的那一行,现在它变为:

TokenValidationParameters = new 
System.IdentityModel.Tokens.TokenValidationParameters
{   
    // map the claimsPrincipal's roles to the roles claim
    RoleClaimType = 
        "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
},

@juunas - 非常感谢你的帮助。

答案 2 :(得分:0)

默认情况下,声明映射将以旧格式映射声明名称,以适应较早的SAML应用程序。默认映射是“ http://schemas.microsoft.com/ws/2008/06/identity/claims/role”而不是“角色”。因此,您可以使用以下代码:

public void ConfigureServices(IServiceCollection services)
{
    // This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
    JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
    ...
}