【问题标题】:What is the verifier in DotNetOpenAuth's DesktopConsumer ProcessUserAuthorization?DotNetOpenAuth 的 DesktopConsumer ProcessUserAuthorization 中的验证者是什么?
【发布时间】:2012-06-13 18:18:24
【问题描述】:

我是 DotNetOpenAuth 的新手,我找不到在 ProcessUserAuthorization 中用作验证器的值。

我想要实现的是使用我的用户凭据登录到使用 OAuth 的应用程序(称为 UserVoice)。这是我的代码的样子:

string requestToken;
var authorizeUri = consumer.RequestUserAuthorization(new Dictionary<string, string>(), null, out requestToken).AbsoluteUri;
var verifier = "???";
var accessToken = consumer.ProcessUserAuthorization(requestToken, verifier).AccessToken;
consumer.PrepareAuthorizedRequest(endpoint, accessToken, data).GetResponse();

我尝试使用我的用户名、密码、消费者密钥和消费者密码,但似乎没有任何效果。有人知道我应该使用哪个值作为验证器吗?

谢谢

【问题讨论】:

    标签: c# oauth dotnetopenauth


    【解决方案1】:

    我终于找到了一种使用 DotNetOpenAuth 登录 UserVoice 的方法。我认为 UserVoice 的 OAuth 实现不是标准的,但在此期间我能够做到:

    var consumer = new DesktopConsumer(this.GetInitialServiceDescription(), this._manager)
    string requestToken;
    consumer.RequestUserAuthorization(null, null, out requestToken);
    
    // get authentication token
    var extraParameters = new Dictionary<string, string>
    {
        { "email", this._email },
        { "password", this._password },
        { "request_token", requestToken },
    };
    
    consumer = new DesktopConsumer(this.GetSecondaryServiceDescription(), this._manager);
    consumer.RequestUserAuthorization(extraParameters, null, out requestToken);
    

    GetInitialServiceDescription 返回良好的请求描述,GetSecondaryServiceDescription 是破解版本,返回授权端点代替请求令牌端点。以这种方式返回的“request_token”(根据我对 OAuth 的理解,这实际上不是一个正常的 request_token)可以用作 PrepareAuthorizedRequest 的访问令牌。

    【讨论】:

      【解决方案2】:

      验证器是在用户表示要授权您的应用后,UserVoice 将在屏幕上显示的代码。用户必须将这个验证器代码从网站复制并粘贴回应用程序的 GUI,以便它可以将其传递给 ProcessUserAuthorization 方法。

      这仅在 OAuth 1.0a(不是 1.0)中是必需的,用于缓解在 1.0 中发现的某些可利用攻击。在您的ServiceProviderDescription 中确保您指定该服务是 1.0a 版本(如果实际上 Uservoice 支持),以便 DNOA 将与 Uservoice 沟通它应该创建一个验证代码。

      顺便说一句,包括扫描进程标题或在您自己的应用程序中托管浏览器在内的各种技巧可以通过让您的应用程序自动为他复制验证码来消除手动用户复制验证码的步骤。

      【讨论】:

        【解决方案3】:

        当通过 WebAPI 完成授权并且您没有在浏览器中显示重定向时,也会使用验证器。在这种情况下,您只需通过代码发送您的 AuthentificationRequest 并将验证器作为 json 字符串获取,而无需任何用户交互。

        在这种情况下,流程(对于 OAuth 1.0)如下所示:

            public void AccessAPI ()
            {
                InMemoryOAuthTokenManager tokenManager = InMemoryOAuthTokenManager(YOUR_CLIENT_KEY, YOUR_CLIENT_SECRET);
                var consumer = new DesktopConsumer(GetAuthServerDescription(), tokenManager);
        
                    // Get Request token
                    string requestToken;
                    var parameters = new Dictionary<string, string>();
                    parameters["email"] = "foo";
                    parameters["password"] = "bar";
                    Uri authorizationUrl = consumer.RequestUserAuthorization(null, parameters, out requestToken);
        
                    // Authorize and get a verifier (No OAuth Header necessary for the API I wanted to access)
                    var request = WebRequest.Create(authorizationUrl) as HttpWebRequest;
                    request.Method = "Get";
                    request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
                    var response = request.GetResponse() as HttpWebResponse;
                    string verifier = new StreamReader(response.GetResponseStream()).ReadToEnd().Split('=')[1]; //Irgendwie will Json nicht parsen
        
                    // Use verifier to get the final AccessToken
                    AuthorizedTokenResponse authorizationResponse = consumer.ProcessUserAuthorization(requestToken, verifier);
                    string accessToken = authorizationResponse.AccessToken;
        
                    // Access Ressources
                    HttpDeliveryMethods resourceHttpMethod = HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;
                    var resourceEndpoint = new MessageReceivingEndpoint("https://api.discovergy.com/public/v1/meters", resourceHttpMethod);
                    using (IncomingWebResponse resourceResponse = consumer.PrepareAuthorizedRequestAndSend(resourceEndpoint, accessToken))
                    {
                        string result = resourceResponse.GetResponseReader().ReadToEnd();
                        dynamic content = JObject.Parse(result);
                    }
            }
        
            private ServiceProviderDescription GetAuthServerDescription()
            {
                var authServerDescription = new ServiceProviderDescription();
                authServerDescription.RequestTokenEndpoint = new MessageReceivingEndpoint(YOUR_REQUEST_ENDPOINT, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest);
                authServerDescription.UserAuthorizationEndpoint = new MessageReceivingEndpoint(YOUR_AUTHORIZATION_ENDPOINT, HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest);
                authServerDescription.AccessTokenEndpoint = new MessageReceivingEndpoint(YOUR_TOKEN_ENDPOINT, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest);
                authServerDescription.ProtocolVersion = ProtocolVersion.V10;
                authServerDescription.TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() };
                return authServerDescription;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-21
          • 1970-01-01
          • 2021-02-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多