0

我编写了这个方法来测试凭据,但我不知道为什么当它进入 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;
4

1 回答 1

0

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

于 2010-07-06T17:44:36.773 回答