0
方法は、ロール=管理者で固定されてい

WEBAPI使用は、Windows認証と主張

私は主張 admin

に注入され Individual User Accountが選択されているWEBAPIプロジェクトとの主張を使用するように成功しています
[Authorize(Roles = "admin")] 
public class ValuesController : ApiController 
{ 

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

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); 
    } 
} 
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; 
    } 
} 

は、今私はIAuthenticationFilterが実装されているWindows authenticationオプションを使用してテストしたいです

とクラスwebapiconfigに追加:

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

     ... 
    } 
} 

をWEBAPIプロジェクトのデバッグ時に請求adminUser.Identity.Claimsである、しかし、それは方法/ APIで許可することができませんでした/値/取得します。

答えて

0

RoleClaimTypeidentity/claims/groupsidであり、roleではありません。

enter image description here

enter image description here

ClaimsIdentityコンストラクタでidentity/claims/roleRoleClaimTypeを設定することにより、我々はそれがここ

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); 
    } 

[Authorize(Roles = "admin")]が新しいアイデンティティである渡す得ることができます

関連する問題