0

Je rencontre un problème avec Azure AAD appRoles et MVC, j'ai modifié le manifeste en ajoutant quelques rôles et les ai assignés à quelques utilisateurs. Toutefois, lorsque j'essaie d'utiliser User.IsInRole ou ClaimsPrincipal.Current.IsInRole, il renvoie toujours false.Azure AAD ClaimsPrincipal IsInRole renvoie toujours false

Click Here to see

Le rôle est d'être de retour dans le JSON des revendications dans la capture d'écran ci-dessus {rôles: SuperAdmin}.

J'ai fait beaucoup de lecture et pour autant que je peux voir que je fais tout correctement mais ne peux pas trouver une raison pourquoi?

Ci-dessous mon 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); 
        } 
       } 
      }); 
    } 
} 
+0

Je ne peux pas reproduire votre problème. Vous pouvez essayer [exemple de code] (https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims) pour vérifier si cela fonctionne ou fournir plus de détails pour aider à reproduire cela. –

+0

Je peux reproduire en utilisant l'exemple de code si vous activez "App Service Authentication" les rôles ne fonctionnent pas – RingoCrazy

Répondre

0

Puisque vous utilisez OpenID Connect Owin middleware pour vous connecter les utilisateurs d'Azure AD, vous n'a pas besoin d'activer la fonctionnalité d'authentification service App/d'autorisation, Cette fonctionnalité permet à votre application de se connecter aux utilisateurs afin de ne pas avoir à modifier le code sur le backend de l'application. Il suffit de désactiver la fonctionnalité Authentification/autorisation du service d'application.