此代码测试凭据有问题吗?

时间:2010-07-06 17:40:49

标签: c# credentials webrequest httpwebresponse

我写了这个方法来测试凭据,但我不知道为什么当它转到GetResponse方法实际上是运行web服务。我只是想让它测试该Web服务的凭据是否正确。

   private bool TestCredentials(string sURL, ref  string sUsername, ref  string sPassword)
    {

        bool retVal = false;
        CredentialCache myCache = new CredentialCache();
        myCache.Add(new Uri(sURL), "Full", new NetworkCredential(sUsername, sPassword, "our_domain"));

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
        request.Credentials = myCache;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
            retVal = true;
        return retVal;

1 个答案:

答案 0 :(得分:0)

如果没有与服务器进行某种通信,您无法知道凭据是否良好。您可以创建一个虚拟URL来执行测试请求。 IE,您可以请求的URL,实际上并不在服务器上执行任何操作。