azure - 检查 MVC 应用程序中的用户角色以获取 Azure Active Directory 的授权

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

这是我在 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 View 中,我正在检查 @User.IsInRole("CBUser") ,它返回 True,因为用户具有 CBUSer 角色。所有这些代码都可以在具有 Azure AD 身份验证和授权的 Visual Studio 中正常运行。但是当我将应用程序移动到 Azure 时,@User.IsInRole("CBUser") 总是返回 false。如何在 MVC View 或 Controller 中读取用户角色。我尝试使用下面的代码来读取用户角色,该代码在 VS2015 中调试时工作正常。但是一旦应用程序移至 Azure 环境,该代码就无法工作

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

最佳答案

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

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

关于azure - 检查 MVC 应用程序中的用户角色以获取 Azure Active Directory 的授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962189/

相关文章:

node.js - 在 Azure 应用服务 (Linux) 上运行的 Node/Next/React 应用程序是否需要 web.config 文件?

azure - Azure 规模集中 Windows VM 上的 Windows 自动更新

c# - 将 Entity Framework 对象作为 JSON 返回

各种订阅中的服务主体可以访问 Azure Keyvault

Azure 服务主体 - API 权限与 AD 角色

Azure 数据工厂管道

azure - Azure 托管逻辑应用程序可以连接到本地 Azure DevOps 服务器(以前称为 Team Foundation Server)

c# - mvc 应用程序中的表单例份验证,使用外部服务进行身份验证

asp.net-mvc-4 - MS 身份和访问工具 MVC 4

azure - OAuth 2.0 SAML 承载断言流程