1

我在 .NET 3.5 解决方案上使用 DotNetOpenAuth 为我们的客户提供社交登录。我成功地实现了 Facebook、Google 和 Twitter,但我被困在了亚马逊。我对 OAuth 2.0 提供商使用相同的代码,但是当亚马逊加载登录屏幕时,我输入了用户名和密码,当我单击登录时,我的代码在调用 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"]));

在亚马逊登录屏幕上单击登录后,在第二次调用此方法时会引发异常。

我尝试了 google-ing,但找不到指向我的问题的资源。

感谢帮助 :)

4

1 回答 1

1

我的解决方案是从来自亚马逊的请求中删除查询字符串中的额外参数。我只是在 IIRF 中放置了一个重写规则,我删除了参数范围。

例如。https://mydomain.com/login?code=xxx&scope=xxx&state=xxx必须是 https://mydomain.com/login?code=xxx&state=xxx

这是必需的,因为 DotNetOpenAuth 使用传入请求来请求授权令牌,但将范围保留在查询字符串中,而亚马逊不喜欢这样。接受的参数是:grant_type、code、client_id、client_secret 和 redirect_uri,任何额外的,您的请求都会得到 400 响应(错误请求)。我尝试编译自己的 DotNetOpenAuth dll,但没有运气,因为它们已签名,我需要使用 .net 3.5 的代码。

作为结论,任何对具有其他参数而不是 Amazon 接受的参数的令牌的请求都将返回 400 响应。

我不知道这是否是最好的选择,但这适用于我的情况。

于 2014-07-02T09:50:10.117 回答