Twitter的:在C#中验证用户名和密码 [英] Twitter: verifying username and password in C#

查看:251
本文介绍了Twitter的:在C#中验证用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

赏金问题

我使用C#3.5窗口窗体应用程序。我使用的接受的答案中提到的code。而我得到下面的错误

I am using c# 3.5 Window Forms Application. I am using the code mentioned in the accepted answer. and I am getting below error

样品code,以验证用户名和密码是真的AP preciated

赏金问完

我有以下使用情况的应用程序:当用户第一次开始使用该应用程序,他输入自己的用户名和密码。然后,在更晚的阶段时,应用程序可能会更新其状态。

I have an application with the following use-case: when the user first starts using the application, he inputs his username and password. Then, at a much later stage, the application may update his status.

目前我使用Twitterizer,但我相信这个问题已经超出了我使用的特定库的范围。以下是code的两个相关行:

Currently I'm using Twitterizer, but I believe the question is beyond the scope of the specific library I'm using. Following are the two relevant lines of code:

Twitter twitter = new Twitter("username", "password", "source"); 
twitter.Status.Update("update");

如果用户名/密码不正确,Twitter的对象的建设不会抛出异常。这可能是因为没有在该点发送。在另一方面,状态更新不会引发异常,如果用户名/密码无效。

The construction of the Twitter object does not throw an exception if the username/password are incorrect. This is probably because nothing is sent at this point. On the other hand, the status update does throw an exception if the username/password are invalid.

我的问题是,我想在验证用户输入的点的用户名/密码,不要试图当发布更新。

My problem is that I want to validate the username/password at the point of user input, not when trying to post the update.

我如何可以验证,而无需缴纳任何的用户名/密码(在Twitterizer或其他方式)?

How can I validate the username/password without posting anything (in Twitterizer or otherwise)?

推荐答案

拍摄快速浏览一下verify_credentials API由peSHIr提到的,我写了一个小程序,这似乎这样的伎俩。这是晚了,但我能对它进行测试了几次,似乎工作。

Taking a quick look at the verify_credentials API as mentioned by peSHIr, I wrote a little routine which seems to do the trick. It's late, but I was able to test it a couple of times and seems to work.

在我的功能,我只是返回true,如果我我得到了Htt presponse code.OK,假,如果我得到任何东西,或抛出一个异常。如果Twitter不喜欢的UID /密码的异常会导致401错误被抛出(未授权)。

In my function, I am just returning true if I I get an HttpResponseCode.OK, and false if I get anything else or an exception is thrown. If twitter does not like the uid/password an exception will be thrown with a 401 error (not authorized.)

public bool CheckTwitterCredentials(string UserName, string Password)
{
    // Assume failure
    bool Result = false;

    // A try except block to handle any exceptions
    try {
        // Encode the user name with password
        string UserPass = Convert.ToBase64String(
            System.Text.Encoding.UTF8.GetBytes(UserName + ":" + Password));

        // Create our HTTP web request object
        HttpWebRequest Request = 
            (HttpWebRequest)WebRequest.Create("http://twitter.com/account/verify_credentials.xml");

        // Set up our request flags and submit type
        Request.Method = "GET";
        Request.ContentType = "application/x-www-form-urlencoded";

        // Add the authorization header with the encoded user name and password
        Request.Headers.Add("Authorization", "Basic " + UserPass);

        // Use an HttpWebResponse object to handle the response from Twitter
        HttpWebResponse WebResponse = (HttpWebResponse)Request.GetResponse();

        // Success if we get an OK response
        Result = WebResponse.StatusCode == HttpStatusCode.OK;
    } catch (Exception Ex) {
        System.Diagnostics.Debug.WriteLine("Error: " + Ex.Message);
    }

    // Return success/failure
    return Result;
}

这篇关于Twitter的:在C#中验证用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆