Problem with this code to test the Credentials?
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;
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.
精彩评论