How to request permission like System.Net.WebPermission in ASP.NET MVC?

I created a small web service that creates an HttpWebRequest to another website using a proxy server and after I used the proxy I got this error:

A permission request of type 'System.Net.WebPermission, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' failed.

I have no idea how to fix this.

Here is my code

 HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://website.com"); req.CookieContainer = c; req.Proxy = new WebProxy("IP:PORT"); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.4; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2)"; req.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; req.Headers.Add("Accept-Language", "en-US"); req.ServicePoint.Expect100Continue = false; req.AllowAutoRedirect = false; req.Timeout = 10000; req.Method = "GET"; req.KeepAlive = true; req.ProtocolVersion = HttpVersion.Version10; 
+4
source share
3 answers

It seems that you are facing a trust issue. Perhaps you can solve the problem by including in your Web.Config , under System.Web ,

 <trust level="Full" /> 

WebPermission not available in a medium trust environment (and you can see this article to change this behavior and for reasons why you shouldn't).

However, if you require full trust , you may need to review the requirements of your application.

+2
source

go to the properties of your project / application (WPF)

In security mode, select enable clickone securitysetting. and set the confidence level of the project

0
source

I also had this problem - and it turned out because the dll was executing a web request, loading from the Internet.

I had to go into the dll properties in the file explorer (right-click> Properties) and click "Unblock" - be sure to click "Apply" and then "Good."

0
source

All Articles