这是我在 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/