0

Le procédé a été obtenu avec des rôles = admin:WebAPI utilisation revendique avec des fenêtres authentification

[Authorize(Roles = "admin")] 
public class ValuesController : ApiController 
{ 

    // GET api/values   
    public IEnumerable<string> Get() 
    { 
     return new string[] { "value1", "value2" }; 
    }} 

Je suis d'utiliser avec succès les revendications avec le projet WebAPI où Individual User Account est sélectionné lorsque la demande admin est injecté dans

public class ApplicationUser : IdentityUser 
{ 
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, authenticationType); 

     // Add custom user claims here 
     userIdentity.AddClaim(new Claim(ClaimTypes.Role, "admin")); 

     return userIdentity; 
    } 
} 

maintenant, je veux tester avec option Windows authentication où IAuthenticationFilter est mis en œuvre:

public class CustomAuthenticationFilter : IAuthenticationFilter 
{ 
    public bool AllowMultiple { get { return true; } } 
    public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) 
    { 
     var windowsPrincipal = context.Principal as WindowsPrincipal; 
     if (windowsPrincipal != null) 
     { 
      var name = windowsPrincipal.Identity.Name; 
      // TODO: fetch claims from db (i guess based on name) 
      var identity = new ClaimsIdentity(windowsPrincipal.Identity); 

      identity.AddClaim(new Claim(ClaimTypes.Role, "admin")); 

      var claimsPrincipal = new ClaimsPrincipal(identity); 
      // here is the punchline - we're replacing original windows principal 
      // with our own claims principal 


      context.Principal = claimsPrincipal; 
     } 

     return Task.FromResult(0); 
    } 

    public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken) 
    { 
     return Task.FromResult(0); 
    } 
} 

et ajouté à la classe webapiconfig:

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     // Web API configuration and services 
     config.Filters.Add(new CustomAuthenticationFilter()); 

     ... 
    } 
} 

La demande est en adminUser.Identity.Claims lors du débogage projet WebAPI, mais il ne pouvait pas être autorisée dans la méthode/api/valeurs/get.

Une idée?

Répondre

0

L'identité par défaut RoleClaimType est identity/claims/groupsid qui n'est pas role.

enter image description here

En fixant RoleClaimType-identity/claims/role dans le constructeur ClaimsIdentity, nous pouvons le faire passer [Authorize(Roles = "admin")]

public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) 
    { 
     var windowsPrincipal = context.Principal as WindowsPrincipal; 
     if (windowsPrincipal != null) 
     { 
      var name = windowsPrincipal.Identity.Name; 

      // TODO: fetch claims from db (i guess based on name)     
      var identity = new ClaimsIdentity(windowsPrincipal.Identity, 
       null, 
       "Negotiate", 
       "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", 
       "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"); 
      //identity 

      identity.AddClaim(new Claim(ClaimTypes.Role, "admin")); 

      var claimsPrincipal = new ClaimsPrincipal(identity); 
      // here is the punchline - we're replacing original windows principal 
      // with our own claims principal 


      context.Principal = claimsPrincipal; 
     } 

     return Task.FromResult(0); 
    } 

Voici la nouvelle identité:

enter image description here