2

ID サーバーを使用して認証されたASP.net MVC5クライアント(WEBAPP)があります。私は以下のコードを使ってWeb APIにアクセスしています。アイデンティティ・サーバーASP.netでのリソース認証Identity Serverを使用するWEP API

private async Task<TokenResponse> GetTokenAsync() 
    { 
     var client = new TokenClient(
      "https://localhost:44301/identity/connect/token", 
      "mvc_service", 
      "secret"); 

     return await client.RequestClientCredentialsAsync("sampleApi"); 
    } 

とアクセストークンを使用してから

し1.Getアクセストークン、私は、Web APIが提供アイデンティティサーバーとリソースAuthotizationで固定されているWEBAPI

  var client = new HttpClient();     
      client.SetBearerToken(token); 
      var gg = this.HttpContext; 
      var json = await client.GetStringAsync("http://localhost:50990/jarvis"); 
      var jsonStr = JArray.Parse(json).ToString(); 
      var model = JsonConvert.DeserializeObject<List<User>>(jsonStr); 

を呼んでいますIdentityServerによる(Thinktecture.IdentityModel.Owin.ResourceAuthorization.WebApi)

APIControllerは次のとおりです。

[Authorize] 
public class AuthorizationController : ApiController 
{ 
    [ResourceAuthorize(AuthorizationResources.AdminActions.Create, AuthorizationResources.Admin)] 
    public async Task<IHttpActionResult> Get() 
    { 
     var user = User as ClaimsPrincipal; 

     List<User> usersList = await GetUsers(); 
     return Json(usersList); 
    } 
} 

そして、私のResourceAuthorizeがよう

を下回っているResourceAuthorize

public class APIAuthorization: ResourceAuthorizationManager 
{ 
    public override Task<bool> CheckAccessAsync(ResourceAuthorizationContext context) 
    { 
     var resource = context.Resource.First().Value; 

     if (resource == AuthorizationResources.Admin) 
     { 
      return CheckAdminAccessAsync(context); 
     } 

     if (resource == AuthorizationResources.Editor) 
     { 
      return CheckEditorAccessAsync(context); 
     } 

     if (resource == AuthorizationResources.Reader) 
     { 
      return CheckReaderAccessAsync(context); 
     } 

     return Nok(); 
    } 

    private Task<bool> CheckReaderAccessAsync(ResourceAuthorizationContext context) 
    { 
     return Eval(context.Principal.IsInRole("Admin")); 
    } 

    private Task<bool> CheckEditorAccessAsync(ResourceAuthorizationContext context) 
    { 
     return Eval(context.Principal.IsInRole("Admin")); 
    } 

    private Task<bool> CheckAdminAccessAsync(ResourceAuthorizationContext context) 
    { 
     return Eval(context.Principal.IsInRole("Admin")); 
    } 
} 

私の質問私はでチェックを行うことができますので、私はWEBAPIへの請求を渡すにはどうすればよい

  1. ですAPIAuthorization: Eval(context.Principal.IsInRole( "Admin"))

  2. これはリソース認証を行う正​​しい方法ですか?

+0

基本的に私はデスクトップ開発者です。私はウェブアプリを初めて使っている –

答えて

0
 app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
     { 
      Authority = "https://localhost:44333/core", 
      RequiredScopes = new[] { "profile", "openid" }, 
     });**This is what i was looking for , This solved my problem** 
関連する問題