安全异常:请求失败。 - 实体框架代码优先(Sql Compact 4)
- 我创建了一个包含以下类的程序集 (dll):
Person.cs
[Table("People")]
public class Person
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
DataContext.cs
public class DataContext : DbContext
{
public DbSet<Person> People { get; set; }
public DataContext()
: base("name=DataConnection")
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
Calc.cs
public class Calc
{
public int Sum(int num1, int num2)
{
return num1 + num2;
}
}
使用Nuget 安装了以下软件包:
安装包 EFCodeFirst.SqlServerCompact
- 我创建了一个新的 site MVC 3
我添加了对先前创建的 dll 引用
上的以下操作
我修改了 HomeController
HomeController.cs
public ActionResult Index()
{
ViewBag.Message = "Sample Security Error";
return View();
}
public ActionResult Sum(int num1, int num2)
{
ViewBag.Message = "Sample Security Error";
return View("Index", num1 + num2);
}
添加了一个新的控制器
PeopleController.cs
public ActionResult Index()
{
using (var db = new DataContext())
{
var people = from p in db.People
select p;
return View(people.ToList());
}
}
我用以下代码更改了View/Home
:
Index.cshtml
@model int?
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
@Html.ActionLink("Sum", "Sum", new { num1 = 2, num2 = 4})
@if (Model.HasValue)
{
<p>The value is: @Model.Value</p>
}
else
{
<p>No value</p>
}
新的View/人员
Index.cshtml
@model IEnumerable<Error.SecurityException.Model.Person>
@{
ViewBag.Title = "People";
}
<h2>@ViewBag.Title</h2>
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
在 Web.config 中添加了连接字符串
<connectionStrings>
<add name="DataConnection" connectionString="Data Source=|DataDirectory|Data.sdf;" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
App_Data
文件夹创建了数据库 Data.sdf
与 Person.cs
类的结构相同。
使用 Nuget 安装以下软件包:
安装包 EFCodeFirst.SqlServerCompact
结果
本地 IIS 中的代码运行良好!
求和值
显示人员
错误
当我发布网站时,在尝试显示人员时,显示安全错误消息;
System.Security.SecurityException:请求失败。
总和值通常已完成!
我发布了项目这个地址。
单击 People
菜单您可以看到错误!
压缩示例项目并发布在此地址。
我很感激你的帮助!
- I created an assembly (dll) with the following classes:
Person.cs
[Table("People")]
public class Person
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
DataContext.cs
public class DataContext : DbContext
{
public DbSet<Person> People { get; set; }
public DataContext()
: base("name=DataConnection")
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
Calc.cs
public class Calc
{
public int Sum(int num1, int num2)
{
return num1 + num2;
}
}
Using Nuget installed the following package:
Install-Package EFCodeFirst.SqlServerCompact
- I created a new site MVC 3
I added the dll reference to previously created
I modified the following actions on HomeController
HomeController.cs
public ActionResult Index()
{
ViewBag.Message = "Sample Security Error";
return View();
}
public ActionResult Sum(int num1, int num2)
{
ViewBag.Message = "Sample Security Error";
return View("Index", num1 + num2);
}
Added a new controller
PeopleController.cs
public ActionResult Index()
{
using (var db = new DataContext())
{
var people = from p in db.People
select p;
return View(people.ToList());
}
}
I changed the View/Home
with the following code:
Index.cshtml
@model int?
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
@Html.ActionLink("Sum", "Sum", new { num1 = 2, num2 = 4})
@if (Model.HasValue)
{
<p>The value is: @Model.Value</p>
}
else
{
<p>No value</p>
}
New View/People
Index.cshtml
@model IEnumerable<Error.SecurityException.Model.Person>
@{
ViewBag.Title = "People";
}
<h2>@ViewBag.Title</h2>
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
In Web.config I added the connection string
<connectionStrings>
<add name="DataConnection" connectionString="Data Source=|DataDirectory|Data.sdf;" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
App_Data
folder created the database Data.sdf
with the same structure as the Person.cs
class.
Using Nuget installed the following package:
Install-Package EFCodeFirst.SqlServerCompact
Result
The code in local IIS worked perfectly!
Sum values
Display persons
Error
When I published the site, while trying to display the persons, a security error message is displayed;
System.Security.SecurityException: Request failed.
The sum values is normally done!
I published the project this address.
Clicking the People
menu you can see the error!
Compact the sample project and published at this address.
I appreciate the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于我无法打开您的解决方案,因为它配置为在 IIS 中运行...您可以添加吗?
在您的本地项目 web.config (在 system.web 下)并在本地运行它?你有同样的错误吗?如果你这样做,那就是部分信任问题!
马特
Since I can't open your solution because it's configured to run in IIS... Can you add;
In your local projects web.config (under system.web) and run it locally? Do you get the same error? If you do, its a partial trust issue!
Matt
此错误已在实体框架 4.1 版本中修复!
这是他们的实体框架CF的一个bug
This error was fixed in version 4.1 of the Entity Framework!
It was a bug of their entity framework CF
我来到这里寻找在我自己的 ASP.Net MVC 应用程序中触发的安全异常。虽然我的问题可能不完全相同(它在运行任何调用 DbContext 的页面时引发 System.Configuration.ConfigurationPermission),但我找到了一个适合多种情况的简单解决方案。我更改了 web.config,在自定义部分添加了 requirePermission="false",如下所示:
希望这可以帮助遇到相同问题的人。
I got here looking for a Security Exception that was being fired in my own ASP.Net MVC application. Although my problem might not be exactly the same (it was raising a System.Configuration.ConfigurationPermission when running any page that called DbContext) I found a straightforward solution that could be suitable for many cases. I changed my web.config, adding a requirePermission="false" in the custom section, as follows:
Hope this helps somebody with the same problem.