Help with SocketException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am getting the following SocketException error:

"An attempt was made to access a socket in a way forbidden by its access
permissions"

Here is the code:

HttpWebRequest loginFormRequest = (HttpWebRequest)
WebRequest.Create(Properties.Settings.Default.LoginUrl);
HttpWebResponse loginResponse = (HttpWebResponse)
loginFormRequest.GetResponse();

The login URL works from IE.

I even tried setting WebPermissions & Socket Permissions before calling
this code, but it had no visible effect:
 
Here is the Permissions code that I am currently trying without success:

Regex myRegexAllHttps = new Regex("https://.*");

// Create a WebPermission that gives permissions to all the
hosts containing the same host fragment.
WebPermission myWebPermission = new
WebPermission(NetworkAccess.Connect, myRegexAllHttps);

// Check whether all callers higher in the call stack have been
granted the permission.
myWebPermission.Demand();

// Creates a SocketPermission which will allow the target Socket
to connect with domain specified on Settings.
SocketPermission socketPermission1 = new
SocketPermission(System.Security.Permissions.PermissionState.Unrestricted);
socketPermission1.AddPermission(NetworkAccess.Connect,
TransportType.All, Properties.Settings.Default.Domain,
SocketPermission.AllPorts);
socketPermission1.AddPermission(NetworkAccess.Connect,
TransportType.All, Properties.Settings.Default.IP, SocketPermission.AllPorts);
 
Hi

It did not seem to be .NET security exception, so it perhaps was not
related with .NET CAS setting.
It seems that you are running the code in the ASP.NET application, commonly
the ASP.NET app is same as the login user's account if you did not enable
impersonate. While the IE will use the logon user's account.

So you may try to enable the impersonate to see if that works.
810572 How to configure an ASP.NET application for a delegation scenario
http://support.microsoft.com/?id=810572

306158 How to implement impersonation in an ASP.NET application
http://support.microsoft.com/?id=306158

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Peter Huang" said:
Hi

It did not seem to be .NET security exception, so it perhaps was not
related with .NET CAS setting.
It seems that you are running the code in the ASP.NET application, commonly
the ASP.NET app is same as the login user's account if you did not enable
impersonate. While the IE will use the logon user's account.

So you may try to enable the impersonate to see if that works.
810572 How to configure an ASP.NET application for a delegation scenario
http://support.microsoft.com/?id=810572

306158 How to implement impersonation in an ASP.NET application
http://support.microsoft.com/?id=306158

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

This is NOT an ASP.Net application. It is a Windows Forms application. So
IE is not in the picture. I should be running as my user account which DOES
have Administrative priveleges.

I can access this site from my browser.

- Daniel
 
Hi

So it is strange.
1. I think you may try to run the code below.
"caspol -s off" to turn off the .NET CAS check and run the code again to
see if that works
2. By default HttpWebRequest class will work OK with winform application.
To isolate the problem please try other URL to see if that works.
e.g. www.google.com.
3. To isolate the problem, you may try to create a new winform application,
and add a button, and in the button_click to access to the www.google.com
and then your defined URL to see if that works.
Please perform the test and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi

Did my suggestion help you?
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top