C# .NET - CredentialCache.DefaultCredentials 407 proxy authentication

Asked By Nathan Lee on 19-Nov-06 08:05 PM

Hi guys,

I have an ActiveX/ control created in C# embedded for use in Internet Explorer. It does a httpWebrequest in the background. Everything was working fine until it came across a pilot site that is sitting behind a proxy server that requires authentication and is now coming up with the below. I see from the logs that the control is being seen as anonymous and is being denied internet access.

"(407) Unable to authenticate through proxy"

Now I've seen a lot of examples that suggest using CredentialCache.DefaultCredentials to allow your app the authentication of the logged on user. However, when I attempt to use this (see code below), at runtime I am getting.:-

"Request for the permission of type 'System.Security.Permissions.EnvironmentPermission,mscorlib, Version2.0.0.0, Culture=neutral, PublicKeyToken=xxxx' failed"

I am running .NET2.0 and .NET1.1 and have my own site group policy I am using. I thought it might need "Environment Variables" access, but still didn't work when I added this (nor when I have added everything for access!)

This new code is only in my environment at the moment (haven't deployed to test it at pilot site). Could it be this code will not work because my proxy doesn't require authentication? Would this extra code snippet run ok for home users NOT using a proxy?

I really hope someone can help me or suggest an alternative to get this thing through any proxy authentications encountered.

Thanks in advance

=============================

try

{

objRequest.Method = "POST";

objRequest.ContentLength = sb.Length;

objRequest.ContentType = "application/x-www-form-urlencoded";

objRequest.CookieContainer = new CookieContainer() ;

// Assign the credentials of the logged in user or the user being impersonated.

objRequest.Proxy = System.Net.WebProxy.GetDefaultProxy();

objRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;  //System.Net.WebProxy.GetDefaultProxy().Credentials;

objRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;   //System.Net.WebProxy.GetDefaultProxy().Credentials;

 

Some ideas --

Peter Bromberg replied to Nathan Lee on 19-Nov-06 08:29 PM

First, this doesn't "look like" an ActiveX control (it would be pretty hard to write an ActiveX control in C#). It "Looks like" managed code.

Your default credentials are the credentials of the machine when you log on after you boot your machine (to your office or home network, etc.)

Why would these credentials enable you to gain access to some remote site?

Whatever the credentials are for this site (if you know them) then you would need to configure your proxy with those credentials specifically- at a minimum, a host address and port, and a username and password (an ICredentials object).

Hope that helps.


few more details

Nathan Lee replied to Peter Bromberg on 19-Nov-06 08:40 PM

Thanks for the response Peter.

Yeah, I think it's not really activeX but I guess that's the best way I could describe it, as it's not a web control either.

Hmmm, I may not have explained it too well either. The access I need to code for is for the control run at the client through IE that is trying to get past/outside of THEIR network proxy server so that they can access a remote URL. It is their proxy that is preventing the control from connecting to a remote URL, so what I'm trying to do is give my control the defaultcredentials of the user so that it can do a successful HTTP request through the proxy.

I can't use a solution that requires specific hardcoded username/password/proxy address because this will be run from client machines, so thought using defaultproxy and credential details would be all I need.

Hope that makes it a bit clearer (clear as mud?).

Thanks again.

Very much clearer

Peter Bromberg replied to Nathan Lee on 19-Nov-06 09:15 PM

Unless their proxy is set via Internet Explorer, in which case the "DefaultCredentials" scheme should work, then you'll need to provide some sort of configuration file or other settings that the user can fill in, which your component will be able to read and use to create a working proxy to attach to the HttpWebRequest object before it's use.


slowly getting there
Nathan Lee replied to Peter Bromberg on 19-Nov-06 09:26 PM

Thanks again Peter. So if that is the case (using IE proxy settings is fine), can you explain why I'd be getting the permission issue below at runtime (in my local test environment at the moment, I haven't deployed for client to test). I use a proxy locally but it doesn't require authentication. I'd prefer this code to work regardless if there is a proxy there or not. As I said the control has a "site" security policy (to the remote URL), I have tried giving everything access with still no joy. Frustrating! :-)

"Request for the permission of type 'System.Security.Permissions.EnvironmentPermission,mscorlib, Version2.0.0.0, Culture=neutral, PublicKeyToken=xxxx' failed"

Urgent : Need Help to develop reusable custom cache component
RamaKrishna Tangirala replied to Nathan Lee on 30-Nov-06 04:47 AM

HI,

I want to develop reusable custom cache component.

So  what more functionlities we can give to Custom Cache component other than existing DotNet caching features.

I found the fallowing features to be added to my component :

                1.    Middle Tier Application Cache.

                2.  Maintaining caching object across processesRegards,

Please help me...

Regards,

RamaKrishna