-1

I am trying to setup web application project in visual studio 2012 that my boss just gave me for setup.

When I was opening project in VS by .sln file it gives me following error:

The imported project "..\.nuget\nuget.targets" was not found. Confirm that path in the ,<Import> declaration is correct and that the file exist on disk.

So I found the solution on SO and removed following line form .csproj file:

<Import Project="$(SolutionDir)\.nuget\nuget.targets" />

and reload the project this time it opened correctly but when I built the project it gave following error:

Assembly 'System.Web.Http.Cors, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' uses 'System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

and this is packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AutoMapper" version="3.1.0" targetFramework="net45" />
  <package id="EntityFramework" version="6.0.0-beta1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Cors" version="5.0.0-beta2" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="4.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0-beta2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0-beta2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Cors" version="5.0.0-beta2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.Data" version="2.0.20710.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.WebData" version="2.0.30506.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Mvc4Futures" version="4.0.20710.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net45" />
</packages>

I then installed Microsoft.AspNet.WebApi 4.0.30506.0 and Microsoft.AspNet.WebApi.Cors 5.0.0-beta2 from package manager console.

Now the project built successfully but when I am executing the project, i am getting following error:

Attempt by security transparent method 'System.Web.Http.Cors.EnableCorsAttribute..ctor(System.String, System.String, System.String, System.String)' to access security critical method 'System.Web.Cors.CorsPolicy..ctor()' failed.

Assembly 'System.Web.Http.Cors, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model.  Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.

at this line:

var cors = new EnableCorsAttribute("*", "*", "*");

Please guide me.

Thanx.

user2517610
  • 275
  • 2
  • 8
  • 27

3 Answers3

1

.net framework 4.0 onwards, assembly default to security critical. Solution is to remove AllowPartiallyTrustedCallersAttribute attribute in your assemblies where it is defined or add it to all the the assemblies.

Try adding:

[assembly: AllowPartiallyTrustedCallers()] to your assemblyinfo.cs

Don't forget to add: using System.Security in assemblyinfo.cs file at the start

amit dayama
  • 3,246
  • 2
  • 17
  • 28
  • could you please explain how can i remove this attribute as i searched it in entire solution but couldn't found one. – user2517610 Nov 18 '15 at 05:57
  • it must be in your assemblyinfo.cs .. if it doesn't work then other way would be to change the trust level in web.config to full. Beware of security.. – amit dayama Nov 18 '15 at 06:01
  • I could not find it in AssemblyInfo.cs and couldn't I setup it with the same configuration as it is built. . .without changing security level and other thigns? – user2517610 Nov 18 '15 at 06:08
  • Now getting this error on Application_Start() Operation could destabilize the runtime. – user2517610 Nov 18 '15 at 06:14
  • the error means one of the dlls in your bin fails verification. Try executing PEVERIFY from visual studio command prompt on each dll to find the offender – amit dayama Nov 18 '15 at 06:21
0

Open Visual Studio 2012. Go to File->New Project -> Select ASP.NET MVC 4 Web Application, give a name, add a location path for the solution file, give a name to the solution and then press OK.Hope this will work. If it doesn't work then please update your nuget packages.Right Click on the solution and then click Manage Nuget Packages for Solution and install the required packages for the solution.

  • but i am trying to open a project that already created, not trying to create new project. – user2517610 Nov 18 '15 at 05:39
  • @user2517610 follow this link http://stackoverflow.com/questions/18347290/webapi-odata-5-0-beta-accessing-globalconfiguration-throws-security-error – Shafi Hossain Nov 18 '15 at 05:54
0

Ok,

Thanx everyone.

I resolved the issue by :

  1. Updating "Nuget" from extension manager.
  2. Then in Package Manager Console there appears a "Restore" button.
  3. I restored all the packages and all error gone.

Thanx.

user2517610
  • 275
  • 2
  • 8
  • 27