2017-07-27 7 views
0

に承認のために働く私は、Identity ServerのIDを開くに新しいですし、APIを構築、 、私のサーバーは、クライアントへのアクセストークンを与えます私は、Identity Server 3およびAPIとクライアントを設定しているのはなぜ[ルート]はアイデンティティサーバ3

public void Configuration (IAppBuilder app) 
     { 
      var options = new IdentityServerOptions 
      { 
       Factory = new IdentityServerServiceFactory() 
       .UseInMemoryClients(Clients.Get()) 
       .UseInMemoryScopes(Scopes.Get()) 
       .UseInMemoryUsers(Users.Get()), 

       RequireSsl = false 
      }; 
      app.UseIdentityServer(options); 

     } 

と私のAPIは私が

を使用するときに、なぜ私の質問がある

public void Configuration(IAppBuilder app) 
     { 
      //accept access token from indentityserver and require a scope of api1 
      app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
      { 
       Authority = "http://localhost:62172/", 
       ValidationMode = ValidationMode.ValidationEndpoint, 
       RequiredScopes = new[] { "api1" } 
      }); 
      //config web api 
      var config = new HttpConfiguration(); 
      config.MapHttpAttributeRoutes(); 
      // require authentication for all controllers 
      config.Filters.Add(new AuthorizeAttribute()); 

      app.UseWebApi(config); 
     } 

を起動しているサーバーでのAPI スタートアップを呼び出すときに、それは使用することができています

[Route("api/Search")] 

それは[オーソライズ]

[Route("api/Search")] 
public async Task<IHttpActionResult> Companies(SearchRequest searchRequest) 
{ 
} 

仕事上の理由ブローコードのように使用してのように動作します。これはつまり、私の方法で私のコントローラで

[Authorize] 
public async Task<IHttpActionResult> Companies(SearchRequest searchRequest) 
     {} 

詳細情報

を私は電話をしており、私はユーザーが許可を得ようとしています

public async Task<IHttpActionResult> Companies(SearchRequest searchRequest) 
     { 
      var caller = User as ClaimsPrincipal; 

      if (!ModelState.IsValid) 
      { 
       return BadRequest(ModelState); 
      } 
      HttpResponseMessage response = new HttpResponseMessage(); 
      Framework.BusinessLogicFactory factory = new Framework.BusinessLogicFactory(); 

      BusinessLogic.Search search = factory.CreateSearch(); 
} 

しかし、私は、コントローラの[承認]または[ルート(「API/Sreach」)]属性を持っていない場合は、APIへの呼び出しは戻って結果を取得し、 、これは私が私のAPI

をテストしています方法です
string APiURL = "http://localhost:59791/api/Search"; 
      var responses = GetClientToken(); 
      var clinet = new HttpClient(); 
      var value = new Dictionary<string, string>() 
      { 

       { "CompanyNumber", " " }, 
       { "CompanyName", "test" }, 
       { "Address1", " " }, 
       { "Address2", " " }, 
       { "Address3", " " }, 
       { "PostCode", " " }, 
       { "CountryCode", " " }, 

      }; 
      var content = new FormUrlEncodedContent(value); 
      var response = await clinet.PostAsync(APiURL, content); 
      var t = response.StatusCode; 
+0

私の質問にマークが付いている理由を教えてください。 –

答えて

1

APIスタートアップにはconfig.Filters.Add(new AuthorizeAttribute());行があるため、

これは、すべてのコントローラに[Authorize]を適用します。したがって、許可を引き起こしているが、このフィルタは[Route]ではありません。

+0

設定のせいであれば、[経路]または[権限]なしで動作しませんか? [route]または[authorize]がないときに承認が行われず、[route]または[authorize]があるときに承認が発生しました –

+0

コントローラからすべての属性を削除すると、コントローラは引き続きコントローラに承認。 –

+0

いいえ、コントローラにアトリビュートがない場合、承認が機能しません。そのため、承認の原因がわからないのはなぜですか? –

関連する問題