2016-06-11 7 views
1

OAuthを使用するWeb API用のスワッガードキュメントをセットアップしようとしています(OWINミドルウェア 'UseOAuthAuthorizationServer ')しかし問題に走っている。Web API(v5.2.3)でSwagger(Swashbuckle&Swashbuckle.Coreバージョンは5.3.2)を使用していますが、クライアントIDはnullです

私は以下のようにOAuthの闊歩の設定を行っている:

  c.OperationFilter<AssignOAuth2SecurityRequirements>(); 

闊歩UI構成:

スコープと

一般のOAuth設定:選択したAPIのOAuthを有効にする

    c.OAuth2("oauth2") 
        .Description("OAuth2 Implicit Grant") 
        .Flow("implicit") 
        .AuthorizationUrl(ConfigurationManager.AppSettings[Constants.AuthorizationHostUrl]) 
        .TokenUrl(ConfigurationManager.AppSettings[Constants.TokenUrl]) 
        .Scopes(scopes => 
        { 
         scopes.Add("Admin", "Admin permission required"); 
        }); 

  c.EnableOAuth2Support("MyPortal", "test-realm", "Swagger UI"); 

上記のコードでは、適切なエンドポイントメソッドでトグルボタンを表示できます。ボタンをクリックすると、上記の設定に従ってスコープが表示されます。

一度スコープを選択して認証ボタンを押すと、OAuthサーバーはクライアントを最初に検証してクライアントID値を渡していないように見えるため、認証に失敗します。問題は、私のOAuthサーバーで検出された、すなわち、

OAuthAuthorizationServerProvider::ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 

方法は、ライン以下で失敗:

 if (!context.TryGetBasicCredentials(out suppliedClientId, out suppliedClientSecret)) 
     { 
      //if resource provider taking client credentials with FORMS authentication 
      context.TryGetFormCredentials(out suppliedClientId, out suppliedClientSecret); 
     } 

     if (context.ClientId == null) 
     { 
      context.SetError("invalid_clientId", "client_Id is not set"); 
      return; 
     } 

OAuthValidateClientAuthenticationContext引数は、クライアントに関する情報がありません。

上記の方法でデバッグしている間に、私はそれらのエラーポイントをエスケープし、クライアントを正常に検証しました。

しかし、それでもまだ、私は見当もつかないいるエラーの下になった:

 unsupported_grant_type 

誰かがこれらの問題を解決する方法を私を助けることができますか?

答えて

0

あなたは、クライアントIDを渡すところ、私は見ることができない私のために、次の作品:。

試してみてください。

c.EnableOAuth2Support("your Client Id here", "test-realm", "Swagger UI"); 

代わりの

c.EnableOAuth2Support("MyPortal", "test-realm", "Swagger UI"); 

AssignOAuth2SecurityRequirements操作フィルタすべきですこのように見えます。お客様のクライアントIDを指定した場所に置く必要があります。

public class AssignOAuth2SecurityRequirements : IOperationFilter 
    { 
     public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
     { 
     var actFilters = apiDescription.ActionDescriptor.GetFilterPipeline(); 
     var allowsAnonymous = actFilters.Select(f => f.Instance).OfType<OverrideAuthorizationAttribute>().Any(); 
     if (allowsAnonymous) 
      return; 

     if (operation.security == null) 
      operation.security = new List<IDictionary<string, IEnumerable<string>>>(); 

     var oAuthRequirements = new Dictionary<string, IEnumerable<string>> 
     { 
      {"oauth2", new List<string> {"your client id here"}} 
     }; 

     operation.security.Add(oAuthRequirements); 
     } 
    } 
+0

返信いただきありがとうございます。私はこの記事[リンク](http://www.wmpratt.com/part-ii-swagger-and-asp-net-web-api-enabling-oauth2/)に従いました。その記事の通り、私はスコープを私の役割の名前。あなたの返事の後、私はクライアントの-id(テスト目的のためだけ)にDictionary値をハードコードしましたが、依然としてnullとしてクライアントIDを取得しています。 –

関連する問題