【问题标题】:Azure AAD ClaimsPrincipal IsInRole always returns falseAzure AAD ClaimsPrincipal IsInRole 始终返回 false
【发布时间】:2017-12-18 10:07:51
【问题描述】:

我在使用 Azure AAD appRoles 和 MVC 时遇到问题,我修改了清单,添加了一些角色并将它们分配给几个用户。

但是,当我尝试使用 User.IsInRole 或 ClaimsPrincipal.Current.IsInRole 时,它​​总是返回 false。

Click Here to see

在 {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


【解决方案1】:

由于您使用 OpenID Connect Owin 中间件从 Azure AD 登录用户,因此您无需启用应用服务身份验证/授权功能,该功能为您的应用程序提供了一种登录用户的方式,因此您无需不必更改应用后端的代码。只需关闭应用服务身份验证/授权功能即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多