【问题标题】:Problem with this code to test the Credentials?这段代码测试凭证有问题吗?
【发布时间】:2011-03-12 10:51:03
【问题描述】:

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

【问题讨论】:

    标签: c# credentials webrequest httpwebresponse


    【解决方案1】:

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

    【讨论】:

    • 虚拟网址?你的意思不是传递真正的asmx地址,而是传递例如google.com?但仍然具有相同的凭据?但如果您指的是该服务器上的另一个 url,那么这是我们唯一拥有的 url。
    • 您对服务器有任何控制权吗?如果你不这样做,你可以做的事情并不多。发出请求(IE,调用GetResponse)将执行该操作。我的意思是向 web 服务添加另一个不做任何事情的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多