2014-05-19 2 views
1

내 자신의 사용자 지정 WebApi 인증을 구현하고 싶습니다. ApplicationOAuthProvider에서 GrantResourceOwnerCredentials 메서드를 수정하려고합니다. 인증과 거짓 인증되지 않은 위해에 대한 true를 돌려 내 웹 서비스 호출웹 API 2 OWIN 무기명 토큰 사용자 지정 인증

IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); 

:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     using (UserManager<IdentityUser> userManager = _userManagerFactory()) 
     { 
      IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); 

      if (user == null) 
      { 
       context.SetError("invalid_grant", "The user name or password is incorrect."); 
       return; 
      } 

      ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, 
       context.Options.AuthenticationType); 
      ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, 
       CookieAuthenticationDefaults.AuthenticationType); 
      AuthenticationProperties properties = CreateProperties(user.UserName); 
      AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); 
      context.Validated(ticket); 
      context.Request.Context.Authentication.SignIn(cookiesIdentity); 
     } 
    } 

나는 인증을 교체합니다.

if (authenticated) 
{ 
IdentityUser user = new IdentityUser("username"); 
} 

그러나 ClaimsIdentity를 진행하는 방법을 모르겠습니다. 누구나 참조 할 수있는 샘플이 있습니까? 감사.

답변

0

나는 당신이 찾고있는 것을 정말로 이해하지 못하지만, Thinktect의 OAuth 임베디드 서버 구현을 살펴보고 싶을 수도 있습니다.

체크 아웃 그들의 Thinktecture.IdentityModel 프로젝트 및 샘플 - https://github.com/thinktecture/Thinktecture.IdentityModel

을 그들은 인증을 수행 할 GrantResourceOwnerCredentials 방법을 사용라는 샘플 OAuth2를 \ EmbeddedAuthorizationServer을 \ 샘플 프로젝트가 있습니다.

다음은 ClaimsIdentity에 대한 간단한 블로그 게시물입니다. 도움이 될 수도 있습니다. http://www.remondo.net/simple-claims-based-identity/

1

아직까지는 관련성이 있으므로이 문제를 늦출 수는 있습니다. 티켓에서 클레임을 할당하고 해결하는 방법은 옵션이 있습니다. IUserClaimsStore (documentation for said object)을 구현하거나 단순히 소유권 주장 저장 서비스를 만들 수 있습니다. 가장 수동적 인 방법은 로그인을 확인하고 ID를 검색 한 후 클레임을 사용자에게 명시 적으로 할당하는 것입니다.

using (UserManager<IdentityUser> userManager = _userManagerFactory()) 
    { 
     IdentityUser user = await userManager.FindAsync(context.UserName, context.Password); 

     if (user == null) 
     { 
      context.SetError("invalid_grant", "The user name or password is incorrect."); 
      return; 
     } 

     user.AddClaim(new Claim("sub","whateveryouwantthesubjectnametobe")); 
     user.AddClaim(new Claim("someotherclaim","whosecontentsyoudeterminehoweveryouwouldlike")); 

     ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, 
      context.Options.AuthenticationType); 
     ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, 
      CookieAuthenticationDefaults.AuthenticationType); 
     AuthenticationProperties properties = CreateProperties(user.UserName); 
     AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); 
     context.Validated(ticket); 
     context.Request.Context.Authentication.SignIn(cookiesIdentity); 
    } 

그것을해야한다고, 주장이 토큰의 일부가 될 것입니다 그리고 그것은 다음 요청에 ClaimsPrincipal을 자동으로 채울한다 : 다음 코드를 사용하여 간단한 예입니다. 당신이 요청 티켓의 직렬화를 보려는 경우, ValidateIdentity 방법 간단한 OAuthBearerAuthenticationProvider 구현을 만들고, 오버라이드 (override) : 당신이 당신의 인증을 구성하는 곳 startup.cs에서

private class SuperSecretBearerAuthClass: OAuthBearerAuthenticationProvider 
    { 
     public override Task ValidateIdentity(OAuthValidateIdentityContext context) 
     { 
      var claims = context.Ticket.Identity.Claims; //examine claims here 
      base.ValidateIdentity(context); 
      return Task.FromResult<object>(null); 
     } 
    } 

등록을/같을 것이다 :

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions() 
{ 
     Provider = new SuperSecretBearerAuthClass() 

    }); 
관련 문제