安全异常:请求失败。 - 实体框架代码优先(Sql Compact 4)

发布于 2024-11-23 22:41:50 字数 3526 浏览 5 评论 0原文

  1. 我创建了一个包含以下类的程序集 (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

  1. 我创建了一个新的 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.sdfPerson.cs 类的结构相同。

使用 Nuget 安装以下软件包:

安装包 EFCodeFirst.SqlServerCompact

结果

本地 IIS 中的代码运行良好!

求和值

求和值

显示人员

Dados do Data.sdf

错误

当我发布网站时,在尝试显示人员时,显示安全错误消息;

System.Security.SecurityException:请求失败。

总和值通常已完成!

我发布了项目这个地址。

单击 People 菜单您可以看到错误!

压缩示例项目并发布在此地址

我很感激你的帮助!

  1. 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

  1. 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

Sum value

Display persons

Dados do Data.sdf

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-11-30 22:41:50

由于我无法打开您的解决方案,因为它配置为在 IIS 中运行...您可以添加吗?

<trust level="Medium"/> 

在您的本地项目 web.config (在 system.web 下)并在本地运行它?你有同样的错误吗?如果你这样做,那就是部分信任问题!

马特

Since I can't open your solution because it's configured to run in IIS... Can you add;

<trust level="Medium"/> 

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

離人涙 2024-11-30 22:41:50

此错误已在实体框架 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

秋千易 2024-11-30 22:41:50

我来到这里寻找在我自己的 ASP.Net MVC 应用程序中触发的安全异常。虽然我的问题可能不完全相同(它在运行任何调用 DbContext 的页面时引发 System.Configuration.ConfigurationPermission),但我找到了一个适合多种情况的简单解决方案。我更改了 web.config,在自定义部分添加了 requirePermission="false",如下所示:

  <configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 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:

  <configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

Hope this helps somebody with the same problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文