using Socket is ASP.NET
-
i am trying to use socket in ASP.NET application. application is hosted on my own pc. when it tries to execute SendTo() function of Socket class i get exception that "An attempt was made to acess a socket in a way forbidden by its access permissions" please guide me how to use sockets in ASP.NET application. do i need to set some specific permission or what else??? thanks a lot!! Shoaib
-
i am trying to use socket in ASP.NET application. application is hosted on my own pc. when it tries to execute SendTo() function of Socket class i get exception that "An attempt was made to acess a socket in a way forbidden by its access permissions" please guide me how to use sockets in ASP.NET application. do i need to set some specific permission or what else??? thanks a lot!! Shoaib
As the exception states, the problem is with code-access security. The ASPNET account (or whichever account ASP.NET is running as) was not granted the code-access permission (note, not domain / NT permission). This problem isn't easy to solve and requires that you read about code-access security in the .NET Framework SDK documents. I understand it well, but I can only explain so much (due to time, space, and avoiding confusion of the readers). Basically, code-access security limits code (attributed with security permission attribites) based on evidence for an assembly and a code-access policy. Evidence is information about the assembly and where it came from, such as a URL (http:, https:, file:, etc.), site (servername, either DNS- or WINS-resolved), its strong-name (assembly name, version, culture, and public key token; or, a subset of those), an optionally X.509 signature, etc. This evidence can be used either by the CLR (Common Language Runtime, the runtime for .NET) or by security permission attributes themselves to grant the right to execute (or remove the right, for that matter) to a caller or the entire call stack. A permission set defines various security permissions and a set of resources or actions those permissions grant. A code group defines a condition for code (such as a matching site or URL, or a matching strong name) to meet in order to have the permissions contained in the permission set assigned to the code group. So, the problem you're exhibiting is that your assembly or the ASP.NET user account is not being granted the appropriate permission sets (namely, the
SocketPermission
). This could be because you're not signing your assembly (see the sn.exe utility, and theAssemblyKeyFileAttribute
documentation) or because the machine.config file doesn't give ASP.NET files full trust permissions (or, at least theSocketPermission
). If your site is hosted by another company, you can ask them to allow your code theSocketPermission
, but you should sign it as this opens your code up for new possibilities, including inreased security. Changes are, they may not unlesss you have a good reason because making socket connections from ASP.NET is dangerous (since it opens ASP.NET - and hence IIS and possibly the whole operating system with somewhat elevated privileges if not setup right) and time-consuming since a page can finish execution until the connection is closed (since HTTP - and hence ASP.NET - is stateless). I know this is