HttpWebRequest DefaultNetworkCredentials give error 401
Asked Answered
B

1

6

Im trying to request a url that require windows authentication using HttpWebRequest, I was using

request.Credential = New NetworkCredential("username", "password");

everything was working find until I decide not to hardcord the username and password, and switch to

request.Credential = CredentialCache.DefaultNetworkCredentials;

then i got 401 unauthenticated error. I checked my user with

WindowsIdentity.GetCurrent();

It returns my username correctly, but the

CredentialCache.DefaultNetworkCredentials;

return me empty string for username, domain... pretty much everything is empty string.. Too bad that the HttpWebRequest.Credential is expecting ICredential means i can't set WindowsIdentity.GetCurrent() to it.

Is there anyway that i could pass my current login user to the HttpWebRequest.Credential??

Is CredentialCache.DefaultNetworkCredentials; the correct way? or im just missing some settings?

I had read through some artical like this one http://msdn.microsoft.com/en-us/library/ms998351.aspx.

It is either too long and too complicated for me to understand or it doesn't work. Im new to this, hope to get some straight foward answer here.

Bus answered 30/7, 2013 at 9:24 Comment(5)
I know it's not an answer, but I have two questions: do you even need to set any credentials? And what's wrong with simply passing variables containing username and password (which values you get from wherever you want) to the NetworkCredential constructor instead of hardcoding them as strings?Gironde
because i don't have the username and the password, and users are not happy with every single request they made, they had to enter username and password, I was storing the username and password on the login screen in session previously, but my boss aren't happy with that, he thinks it is not a good practice for a security. So, here i am, asking this questionBus
I have this exact same issue... ughAccommodative
I am having same issue...Bellinger
The question should really state whether Fiddler is in the middle, here.Kong
E
1

If you are using basic authentication, then this will not work. See this question: HttpWebRequest with basic authentication fails with 401 erorr for DefaultNetworkCredentials

And this microsoft article states, that DefaultCredentials just apply to NTLM, negotiate, and Kerberos-based authentication. http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultcredentials.aspx

For information purposes this is an article explaining the authentication methods in short: http://msdn.microsoft.com/en-us/library/ms789031.aspx

Englis answered 30/7, 2013 at 9:35 Comment(1)
Using the localhost.fiddler mechanism has produced the effect in the question for me.Kong

© 2022 - 2024 — McMap. All rights reserved.