2017-12-26 8 views
3

.NET에서 oauth 및 owin의 초보자입니다. 나는 ValidateClientAuthentication 메서드와 GrantResourceOwnerCredentials 메서드를 이해하려고 노력했다. GrantResourceOwnerCredentials 메서드를 사용하여 자격 증명의 유효성을 검사하고 토큰을 생성 할 수 있음을 알고있었습니다. 그런 다음 ValidateClientAuthentication() 메서드의 용도는 무엇입니까? 친절하게 이것에 관해 나를 안내해라. 고마워.OWIN의 oAuth에서 ValidateClientAuthentication 메서드와 GrantResourceOwnerCredentials 메서드의 차이점은 무엇입니까?

public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 
      return Task.Factory.StartNew(() => 
      { 
       var userName = context.UserName; 
       var password = context.Password; 
       var userService = new UserService(); // our created one 
       var user = userService.ValidateUser(userName, password); 
       if (user != null) 
       { 
        var claims = new List<Claim>() 
        { 
         new Claim(ClaimTypes.Sid, Convert.ToString(user.Id)), 
         new Claim(ClaimTypes.Name, user.Name), 
         new Claim(ClaimTypes.Email, user.Email) 
        }; 
        ClaimsIdentity oAuthIdentity = new ClaimsIdentity(claims,Startup.OAuthOptions.AuthenticationType); 


        var properties = CreateProperties(user.Name); 
        var ticket = new AuthenticationTicket(oAuthIdentity, properties); 
        context.Validated(ticket); 
       } 
       else 
       { 
        context.SetError("invalid_grant", "The user name or password is incorrect"); 
       } 
      }); 
     } 
     #endregion 

     #region[ValidateClientAuthentication] 
     public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
     { 
      if (context.ClientId == null) 
       context.Validated(); 

      return Task.FromResult<object>(null); 
     } 
     #endregion 

답변

1

클라이언트와 자원 소유자가 OAuth를 아래 별개의 기관이라는 것을 기억하십시오의 OAuth 2.0 스펙에 Client Credentials FlowResource Owner Password Credentials Flow 관련이있다. 클라이언트는 대신 리소스 소유자 인을 요청합니다.

실제 사용자 이름과 암호를 수락하고 액세스 토큰을 발급 받으려는 경우 GrantResourceOwnerCredentials를 사용하는 것이 좋습니다.

ValidateClientAuthentication을 사용하여 클라이언트의 상태를 확인해야합니다. 클라이언트를 권한 서 v에 등록한 후 권한 서 v가 여전히 유효한지 확인해야하는 경우이를 수행 할 수 있습니다.

내가 본 대부분의 코드 샘플은 예제에서 수행 한 것처럼 context.Validated()를 호출 한 것으로 보입니다. 좀 더 깊이있는 코드 샘플로 블로그 게시물을 찾을 수있었습니다. 여기에서 확인하십시오 : http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/

+0

안녕하세요. 회신 해 주셔서 감사합니다. 위대한 :) 이제 그 방법의 차이점을 이해했습니다. 또한 'validated()'메소드에 대한 이해를 얻었습니다. 참조 링크는 매우 유용합니다. 잘 했어. 그것을 지키십시오. –

+0

나의 겸허 한 제안은 당신이 당신의 좋은 대답을 제공 할 때마다 개념이 간단한 예제로 실시간 예제 나 시나리오로 설명된다면 큰 가치를 더할 것입니다. 도와 줘서 고마워. :) 아프면 내 제안에 대해 미안해. –

관련 문제