【问题标题】:Check Roles of User in MVC Application for Authorization from Azure Active Directory从 Azure Active Directory 检查 MVC 应用程序中用户的角色以获得授权
【发布时间】:2019-03-28 11:12:52
【问题描述】:

这是我在 Startup.Auth.cs 中的代码

 public void ConfigureAuth(IAppBuilder app)        {

       JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer=false,
                    NameClaimType = "upn",
                   RoleClaimType ="roles"   
                }                   
            });
    }

在我的 MVC 视图中,我正在检查返回 True 的 @User.IsInRole("CBUser") ,因为用户具有 CBUSer 角色。所有这些代码在带有 Azure AD Authentication and Authorization 的 Visual Studio 中都能正常工作。但是当我将应用程序移动到 Azure 时,@User.IsInRole("CBUser") 总是返回 false。我如何才能在 MVC 视图或控制器中读取用户角色。我尝试使用下面的代码来读取在 VS2015 中调试时工作正常的用户角色。但是一旦应用程序移动到 Azure 环境就无法工作

            var appRoles = new List<string>();
        foreach (Claim claim in ClaimsPrincipal.Current.FindAll("roles"))
                appRoles.Add(claim.Value);

【问题讨论】:

  • 您是否对部署到 Azure 的应用使用相同的 clientId 值?

标签: azure asp.net-mvc-4 azure-active-directory azure-web-app-service openid-connect


【解决方案1】:

您发布的代码看起来不错。或许你可以拿官方样张看看:Authorization in a web app using Azure AD application roles & role claims

此外,您还可以远程调试您的 Web 应用程序:Troubleshoot a web app in Azure App Service using Visual Studio

【讨论】:

  • 如果您是 Microsoft 合作伙伴,我建议您访问 aka.ms/devchat 以获得有关 Azure 的个性化支持。它是免费的,并且 24x5 全天候运行。
  • 谢谢。我会调查的
猜你喜欢
  • 2018-03-08
  • 1970-01-01
  • 2017-10-10
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多