【发布时间】:2017-12-18 10:07:51
【问题描述】:
我在使用 Azure AAD appRoles 和 MVC 时遇到问题,我修改了清单,添加了一些角色并将它们分配给几个用户。
但是,当我尝试使用 User.IsInRole 或 ClaimsPrincipal.Current.IsInRole 时,它总是返回 false。
在 {roles:SuperAdmin} 上面的屏幕截图中,角色正在以声明的 json 格式返回。
我已经做了很多阅读,据我所知,我做的一切都是正确的,但找不到原因?
下面是我的 Startup.Auth.cs
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
public static readonly string Authority = aadInstance + tenantId;
// This is the resource ID of the AAD Graph API. We'll need this to request a token to call the Graph API.
//string graphResourceId = "https://graph.windows.net";
public void ConfigureAuth(IAppBuilder app)
{
ApplicationDbContext db = new ApplicationDbContext();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
RoleClaimType= "roles"
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
return Task.FromResult(0);
}
}
});
}
}
【问题讨论】:
-
我无法重现您的问题。您可以尝试code sample 来检查它是否有效或提供更多详细信息以帮助重现。
-
如果您打开“应用服务身份验证”角色不起作用,我可以使用示例代码重现
标签: c# azure azure-active-directory claims-based-identity asp.net-mvc-5.2