我使用Azure AD获得了一个web api 2项目。使用[Authorize]属性可以很好地工作,但我无法使用角色。我已将以下内容添加到应用程序清单中:
"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"description": "Access to example controller",
"displayName": "Example",
"id": "76d4bbb5-24c1-45ad-9446-cc360d7bd012",
"isEnabled": "true",
"origin": "Application",
"value": "example"
}
和我的api控制器的这个属性:[Authorize(Roles =“example”)]。如果我删除此属性并调用我的api,则声明包含在ClaimsPrincipal.Current:
中{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: example}
但这还不足以让我的应用程序在使用角色标头时进行身份验证。任何帮助将不胜感激。
答案 0 :(得分:1)
事实证明,令牌是SAML令牌,而不是JWT。因此,为了实现这一点,我只需要在我的TokenValidationParameters中设置它
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters()
{
// we inject our own multitenant validation logic
ValidateIssuer = false,
// map the claimsPrincipal's roles to the roles claim
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
}
答案 1 :(得分:0)
您的ConfigureAuth函数应该是这样
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Audience = ConfigurationManager.AppSettings["ida:Audience"],
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
}
});
}
然后您可以将Authorize属性添加为
[Authorize(Roles = "example")]