开发者

Test Credentials to access a web page

Is there a nice and tested piece of code out there that I can use for this purpose:

get the user/pass and the address of a web service (asmx page) and check if the user/pass are valid or not.

I think I should use HTTPRequest,etc to do that but I do not have a good knowledge on that topic , causing my current method to not working properly.

If there is a good piece of code for this purpose I appreciate for pointing me to that.

Tha开发者_运维问答nks

P.S: I am not using DefaultCredentials in my code. Since I want them to enter user/pass so now I need to be able to TEST their user/pass and show proper message to them if their credentials is not valid.


You can use the HttpWebRequest.Credentials Property (depends on the web service authentication) and the CredentialCache Class.

Also some code examples (from google):
Retrieving HTTP content in .NET
Combine Invoking Web Service dynamically using HttpWebRequest with .Credentials.


public bool TestCredentials(string url, string username, string password)
{
    var web = new WebClient();
    web.Credentials = new NetworkCredential(username,password);

    try
    {   
        web.DownloadData(url);
        return true;
    }
    catch (WebException ex)
    {
        var response = (HttpWebResponse)ex.Response;
        if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            return false;
        }

        throw;
    }   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜