我在.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响应。
我不知道这是否是最佳选择,但这对我而言有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句