Credentials persist after CredentialCache.Remove()

S

Stan Rawrysz

So I've been banging my head against the wall here for a while now and
cannot find any useful information regarding this issue that I'm having.

The problem is that after I create a WebRequest object with a
CredentialCache and make a call to the resource, all subsequent requests
to that resource, even if I don't set the cache to the next WebRequest
and set the cache to null, end up using that NetworkCredential from the
original request.

I've setup a quick unit test to expose this problem:

[Test]
[Explicit]
[ExpectedException(typeof
(WebException))]
public void CredentialCachePersistenceTest()
{
Stream myStream = null;
Uri myAuthTestUri = new Uri(_authTestUrl);
NetworkCredential myCred = new NetworkCredential(_authTestUser,
_authTestPass, _authTestDomain);
CredentialCache myCache = new CredentialCache();
myCache.Add(myAuthTestUri, "Basic", myCred);

WebRequest myReq = WebRequest.Create(myAuthTestUri);
myReq.Credentials = myCache;
WebResponse myResponse = myReq.GetResponse();
myStream = myResponse.GetResponseStream();
Console.WriteLine("Stream1.CanRead = " + myStream.CanRead);
myStream.Close();
myCache.Remove(myAuthTestUri, "Basic");
myResponse.Close();

myCache = null;

WebRequest myReq2 = WebRequest.Create(new Uri(_authTestUrl));
WebResponse myResponse2 = myReq2.GetResponse();
myStream = myResponse2.GetResponseStream();
Console.WriteLine("Stream2.CanRead = " + myStream.CanRead);
}

The web site I'm connecting to is set to have anonymous access off with
"Basic Authentication" and "Integrated Windows Security" on.

Has anyone come accross this problem? Is there a solution?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top