![](/img/trans.png)
[英]How do I debug a 400 Bad Request error with DotNetOpenAuth?
[英]Login with Amazon using DotNetOpenAuth returns (400) Bad Request
我在.NET 3.5解決方案上使用DotNetOpenAuth為我們的客戶提供社交登錄。 我成功地實現了Facebook,Google和Twitter,但我被困在亞馬遜。 我為OAuth 2.0提供程序使用了相同的代碼,但是當Amazon加載登錄屏幕時,我輸入了用戶名和密碼,並且當我單擊登錄時,我的代碼在調用ProcessUserAuthorization()時崩潰,並引發以下錯誤:DNOA抱怨:“發送郵件時發生錯誤直接發送消息或獲得響應。” 內部異常:“遠程服務器返回錯誤:(400)錯誤的請求。”。 這是我的方法:
private static AuthenticationDetailsType LoginOauth2(string[] scope = null, Uri return_uri = null)
{
try
{
// get provider name
string provider_name = ((LoginProviders)providerID).ToString();
var response = new AuthenticationDetailsType();
// get configuration details
var token_endpoint = ConfigurationManager.AppSettings[provider_name + "TokenEndPoint"];
var auth_endpoint = ConfigurationManager.AppSettings[provider_name + "AuthEndPoint"];
var app_key = ConfigurationManager.AppSettings[provider_name + "AppKey"];
var app_secret = ConfigurationManager.AppSettings[provider_name + "AppSecret"];
// instantiate a server
var server = new AuthorizationServerDescription
{
TokenEndpoint = new Uri(token_endpoint),
AuthorizationEndpoint = new Uri(auth_endpoint)
};
// instantiate a client
var client = new WebServerClient(server)
{
ClientIdentifier = app_key,
ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(app_secret)
};
IAuthorizationState auth_state = client.ProcessUserAuthorization();
if (auth_state == null)
{
client.RequestUserAuthorization(scope, return_uri);
}
else
{
response = new AuthenticationDetailsType
{
auth_provider = providerID,
auth_token = auth_state.AccessToken,
auth_token_secret = null,
user_id = null
};
}
return response;
}
catch (Exception ex)
{
ErrorLogger.ErrorLog.WriteError("Error on loging in with provider " + provider_name + " error: "+ ex.StackTrace.ToString());
return null;
}
}
這是我的配置鍵:
<add key="amazonTokenEndPoint" value="https://api.amazon.com/auth/o2/token"/>
<add key="amazonAuthEndPoint" value="https://www.amazon.com/ap/oa"/>
<add key="amazonAppKey" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
<add key="amazonAppSecret" value="xxxxxxxxxxxxxxxxxxxxxxx"/>
<add key="amazonDetailsRequestEndPoint" value="https://api.amazon.com/user/profile"/>
<add key="amazonReturnUri" value="https://localhost/SocialLogin.aspx?providerid=x"/>
這就是我所謂的方法:
return LoginOauth2(new string[] { "profile" }, new Uri(System.Configuration.ConfigurationManager.AppSettings["amazonReturnUri"]));
在Amazon登錄屏幕上單擊“登錄”后,在第二次調用此方法時引發異常。
我嘗試了google-ing,但是找不到指向我問題的資源。
感謝幫助 :)
我的解決方案是從來自Amazon的請求的查詢字符串中刪除額外的參數。 我只是在IIRF中放了一個重寫規則,刪除了參數范圍。
例如。 https://mydomain.com/login?code=xxx&scope=xxx&state=xxx必須是https://mydomain.com/login?code=xxx&state=xxx 。
這是必需的,因為DotNetOpenAuth使用傳入請求來請求授權令牌,但將范圍保留在查詢字符串中,而Amazon不喜歡這樣。 接受的參數是:grant_type,代碼,client_id,client_secret和redirect_uri,其他任何內容,您的請求將獲得400個響應(錯誤請求)。 我嘗試編譯自己的DotNetOpenAuth dll,但沒有成功,因為它們已簽名,我需要為.net 3.5使用代碼。
得出的結論是,對令牌的任何請求都具有不同於Amazon接受的參數的參數,都將返回400響應。
我不知道這是否是最佳選擇,但這對我而言有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.