2017-11-02 5 views
0

IdentityServer3을 사용하여 메소드에 대한 호출을 인증하는 C# API가 있습니다.클라이언트 ID를 기반으로 API 호출 권한 부여

특정 클라이언트가 액세스 할 수있는 호출을 제한하고 싶습니다. 클라이언트가 클라이언트 자격 증명 흐름을 사용하고 있으며 클라이언트 ID를 기반으로 호출 권한을 부여하는 방법을 찾으려고합니다.

이 작업을 수행 할 수있는 방법이 있습니까? 아니면 사용자 지정 필터를 작성해야합니까?

+0

고마워요,하지만 그 링크는 많은 도움이되지 않습니다. 클라이언트 자격 증명 플로우가 작동하지만, 호출 할 수있는 메소드를 제한하는 방법이 필요합니다. –

+1

한 가지 해결책은 .. 모든 클라이언트 ID를 등록하고 액세스를 제한하는 것입니다. API 자체에서. –

답변

1

을 확인할 수 있습니다 필요에 따라 업데이트되는 방법 인증 관리자를 추가하는 방법에 대한 - 당신이 등으로 신원 Server 예제를 보면 "MVC 인증"하나는 시작시 사용자 정의 인증 관리자를 등록한 다음 좋은 간단한 리소스 속성을 사용하여 각 컨트롤러 작업을 보호합니다.

이것은 당신의 시작에 간다

app.UseResourceAuthorization(new AuthorizationManager()); 

그런 다음 사용자 정의 인증 관리자 클래스, 등이 .... 정의 당신이 당신의 컨트롤러 액션은 단순히 좋아하는 보호 할 수있는 후

public class AuthorizationManager : ResourceAuthorizationManager 
{ 
    public override Task<bool> CheckAccessAsync(ResourceAuthorizationContext context) 
    { 
     switch (context.Resource.First().Value) 
     { 
      case "PageAuthCheck": 
       return AuthorizeContactDetails(context); 
      default: 
       return Nok(); 
     } 
    } 

    private Task<bool> AuthorizeContactDetails(ResourceAuthorizationContext context) 
    { 
     switch (context.Action.First().Value) 
     { 
      case "Read": 
       return Eval(context.Principal.Identity.IsAuthenticated); 
      case "Write": 
       return Eval(context.Principal.HasClaim("role", "Admin")); 
      default: 
       return Nok(); 
     } 
    } 
} 

....

[ResourceAuthorize("Read", "PageAuthCheck")] 

분명히 이것은 사용자 클레임 역할 검사를 사용하는 예제 일 뿐이지 만 클라이언트 ID를 기준으로 보호하기 위해 쉽게 변경할 수 있어야합니다.

0

당신이 할 수있는 것은 clientid와 clientSecret입니다. clientid와 clientSecret이 앱에 액세스 할 수 있는지 확인하는 로직을 구현하기 만하면됩니다.

확인이 샘플은이

public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
    { 

     string clientId = string.Empty; 
     string clientSecret = string.Empty; 
     Client client = null; 

     if (!context.TryGetBasicCredentials(out clientId, out clientSecret)) 
     { 
      context.TryGetFormCredentials(out clientId, out clientSecret); 
     } 

     if (context.ClientId == null) 
     { 
      //Remove the comments from the below line context.SetError, and invalidate context 
      //if you want to force sending clientId/secrects once obtain access tokens. 
      context.Validated(); 
      //context.SetError("invalid_clientId", "ClientId should be sent."); 
      return Task.FromResult<object>(null); 
     } 

     using (AuthRepository _repo = new AuthRepository()) 
     { 
      client = _repo.FindClient(context.ClientId); 
     } 

     if (client == null) 
     { 
      context.SetError("invalid_clientId", string.Format("Client '{0}' is not registered in the system.", context.ClientId)); 
      return Task.FromResult<object>(null); 
     } 

     if (client.ApplicationType == Models.ApplicationTypes.NativeConfidential) 
     { 
      if (string.IsNullOrWhiteSpace(clientSecret)) 
      { 
       context.SetError("invalid_clientId", "Client secret should be sent."); 
       return Task.FromResult<object>(null); 
      } 
      else 
      { 
       if (client.Secret != Helper.GetHash(clientSecret)) 
       { 
        context.SetError("invalid_clientId", "Client secret is invalid."); 
        return Task.FromResult<object>(null); 
       } 
      } 
     } 

     if (!client.Active) 
     { 
      context.SetError("invalid_clientId", "Client is inactive."); 
      return Task.FromResult<object>(null); 
     } 

     context.OwinContext.Set<string>("as:clientAllowedOrigin", client.AllowedOrigin); 
     context.OwinContext.Set<string>("as:clientRefreshTokenLifeTime", client.RefreshTokenLifeTime.ToString()); 

     context.Validated(); 
     return Task.FromResult<object>(null); 
    } 

하기이 튜토리얼 here