C# WebRequest.create导致.NET 3.5类库中出现SecurityException,但在.NET 4.5中没有出现

C# WebRequest.create导致.NET 3.5类库中出现SecurityException,但在.NET 4.5中没有出现,c#,.net,C#,.net,以下代码在.NET3.5类库项目中中断,但在.NET4中未中断。我被难住了,如果有任何想法,我将不胜感激: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using NUnit.Framework; namespace test2 { [TestFixture] public class TestClass { [Test]

以下代码在.NET3.5类库项目中中断,但在.NET4中未中断。我被难住了,如果有任何想法,我将不胜感激:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using NUnit.Framework;

namespace test2
{

[TestFixture]
public class TestClass
{
    [Test]
    public void Test()
    {
        var request =
             WebRequest.Create("http://www.google.com");
具体例外情况:

System.Security.SecurityException : Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, ref StackCrawlMark stackMark, Boolean isPermSet)
   at System.Security.CodeAccessPermission.Demand()
   at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
   at System.Net.HttpRequestCreator.Create(Uri Uri)
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(String requestUriString)
   at test2.LaunchRequestInitializeIdsTestFixture.Test()

正如例外情况所说,答案是代码访问安全性(CAS)。我正在从网络驱动器(托管VM)构建代码。在.NET 3.5及以下版本中,网络驱动器的权限较低。对于.NET 3.5 SP1及更高版本,此限制已关闭。以下链接使我走上了正确的轨道:

下面,从.NET 2安装目录中的caspol运行,使我的代码正常工作:
caspol.exe-m-pp off-ag 1-url”file:///Z:/*“完全信任

我从这篇有用的文章中获得了上面的命令行:

供参考: 要恢复到默认值,只需运行“caspol-restore”