2017-03-02 418 views
5

我用Swashbuckle到的WebAPI控制器的文檔。我還使用OAuth2和客戶端憑證流程。所以授權我需要通過client_idclient_secretSwashbuckle OAuth2用戶授權使用客戶端憑證流

我有下面的代碼:

config.EnableSwagger(c => { 
    c.SingleApiVersion("v1", "My API"); 
    c.OAuth2("oauth2") 
     .Flow("application") 
     .TokenUrl("/oauth2/token"); 
    c.OperationFilter<AssignOAuthSecurityRequirements>(); 
}) 
.EnableSwaggerUi(c => { 
    c.EnableOAuth2Support(clientId: "clientIdValue", clientSecret:"clientSecretValue", "", ""); 
    c.CustomAsset("index", Assembly.GetExecutingAssembly(), "WebAPI.Swagger.UI.index.html"); 
}); 

授權工作正常,但我client_idclient_secret值是硬編碼(clientIdValue,clientSecretValue)。如何增加用戶在此對話框中輸入該值的可能性?誰能幫我?

enter image description here

請讓我知道如果我需要張貼的AssignOAuthSecurityRequirements代碼了。感謝所有提前

+0

你解決這個嗎?我正在尋找相同問題的解決方案。 – jks

+0

同上,我也很好奇我怎樣才能獲得一個消費者可以輸入他們的client_id和client_secret進行驗證的對話框。這是可能的大招嗎? – Zoop

回答

0

不知道到底出了什麼錯在你的代碼,也許缺少的範圍定義。

我和ASP.NET的核心和當前版本Swashbuckle.AspNetCore的(https://github.com/domaindrivendev/Swashbuckle.AspNetCore)成功地做到了

憑證流被稱爲「應用程序」的客戶端,這樣,在你Startup.cs文件,您需要配置揚鞭如下:

 services.AddSwaggerGen(c => { 

      //other configs... 

      c.AddSecurityDefinition("oauth2", new OAuth2Scheme { 
       Type = "oauth2", 
       Flow = "application", 
       TokenUrl = "<token_endpoint_url>", 
       Scopes = new Dictionary<string, string> 
       { 
        { "first-scope", "First scope description" }, 
        { "second-scope", "Second scope description" } 
        //define as many scopes as you want... 
       } 
      }); 
     }); 

的TokenUrl參數必須指向一個有效的OAuth 2.0兼容令牌端點(結賬http://docs.identityserver.io/en/release/endpoints/token.html用於終端該如何做/看起來像一個樣品)。我的測試中都使用了絕對網址和相對網址。

之後,授權對話框應該像波紋管:

Authorize popup

  • 請注意,您必須至少選擇一個範圍內授權按鈕實際上提交任何東西(的OAuth的應該組件之前,被改爲添加免責聲明恕我直言)。

任何其他配置被要求在SwaggerUI部分。