测试凭证的代码有问题吗?
我编写此方法是为了测试凭据,但我不知道为什么当它转到 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;
I wrote this method to test for credentials but I dunno why when it goes to the GetResponse method is actually goes and runs the webservice. I just want it to test if the credentials for that web service are correct or not.
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有与服务器进行某种通信,您就无法知道凭据是否有效。您可以创建一个虚拟 URL 来执行测试请求。 IE,您可以请求的 URL,但实际上不会在服务器上执行任何操作。
You can't know if the credentials are good without some kind of communication with the server. You could create a dummy URL to execute a test request against. IE, an URL you can request that doesn't actually do anything on the server.