asp.net-mvc - Azure AD 基于角色的身份验证,User.IsInRole()

标签 asp.net-mvc azure azure-web-app-service azure-active-directory

我正在使用基于 Azure AD 角色的身份验证,我添加了 2 个角色(观察者、读者),这些角色分配给特定用户,工作正常。 list 文件中有这两个新条目。所有身份验证都正常工作,但我无法让 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。我可以在用户的​​调试 session 中看到该声明

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

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

这是我的用户 session 变量

enter image description here 谢谢

最佳答案

{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Observer}

您是如何添加自定义角色的?以下是我添加自定义角色的步骤,供您引用:

  1. 在 Azure AD 上注册应用
  2. 修改其 list 以添加自定义角色,如下所示:

"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"
    }
  ],
  • 通过门户向用户分配角色
  • 使用原始帖子中的代码将 Web 应用与 Azure 集成
  •   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

    关于asp.net-mvc - Azure AD 基于角色的身份验证,User.IsInRole(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41162255/

    相关文章:

    web-services - Azure:将应用程序服务移动到另一个现有的应用程序服务计划

    jquery - 当选择更改时,如何显示下拉菜单的不显眼的 jQuery 验证消息?

    .net - 如何以编程方式获取 WebJob 的日志?

    Azure Blob 下载为字节数组错误 "Memory Stream is not expandable"

    mysql - 如何从 Azure SQL 服务器/数据库连接到远程 MySQL

    visual-studio - 应用程序正在 Azure 创建的 Azure 数据库中运行,但数据未迁移

    azure - 将项目依赖项包含到函数应用程序中

    asp.net - MVC ViewModel 错误 - 没有为此对象定义无参数构造函数

    c# - MVC 获取与发布

    c# - 执行后端电子邮件进程而不让用户等待它完成