我試圖爲使用OAuth的Web API設置swagger文檔(使用OWIN中間件的UseOAuthAuthorizationServer ')但遇到問題。使用Swagger(Swashbuckle和Swashbuckle.Core版本爲5.3.2)時,clientId爲null使用Web Api(v5.2.3)
我做的OAuth招搖配置如下:
一般的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");
});
啓用OAuth使用所選的API:
c.OperationFilter<AssignOAuth2SecurityRequirements>();
揚鞭UI配置:
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
有人可以幫我如何解決這些問題?
感謝您的回覆。我遵循這篇文章[鏈接](http://www.wmpratt.com/part-ii-swagger-and-asp-net-web-api-enabling-oauth2/),並根據該文章,我已將範圍設置爲我的角色名稱。在你回覆之後,我已經將字典值硬編碼到我的客戶端-id(僅用於測試目的),但仍然將客戶端ID設置爲空。 –