【问题标题】:How do I get rid of the SecurityException error when trying to use Automapper in ASP.NET MVC?尝试在 ASP.NET MVC 中使用 Automapper 时如何摆脱 SecurityException 错误?
【发布时间】:2010-05-08 15:10:31
【问题描述】:

我正在使用 NHibernate 开发一个 ASP.NET MVC 应用程序,并且我正在尝试使用 Automapper 从发送到视图的 DTO 对象中隐藏域对象:

  1. ClassLibrary 与我的域(用于 NHibernate)和 DTO 对象
  2. 在我的项目中创建 SessionFactory 和工厂的类库

我下载了 AutoMapper 以在 DTO 中转换域对象,并在 Application_Start 中添加了执行此操作的代码。

当我在 VisualStudio 中运行应用程序(按 F5)时,它运行良好,并且我的 DTO 被映射到视图中。

当我在 IIS 中发布它时,我得到一个安全异常:

Mapper.CreateMap<Category, CategoryDto>(); 
Mapper.CreateMap<Product, ProductDto>();

System.Security.SecurityException:请求“System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限失败。

如何让 Automapper 在没有安全异常的情况下工作?

【问题讨论】:

    标签: asp.net-mvc security iis automapper securityexception


    【解决方案1】:

    右键单击 AutoMapper.dll > 选择属性 > 取消阻止。

    然后重启 IIS

    【讨论】:

    • 我已经完全信任地运行并且感到困惑,直到我意识到我的 AutoMapper.dll 被“阻止”。很好的收获。
    • 我必须从我的项目中删除引用并在解除阻止后重新添加它才能使其正常工作。
    • 谢谢,我解封但没有重置 IIS。现在可以在 iisreset 之后使用。
    • 您必须重新启动 IIS,您不能只是取消阻止并重新编译您的网站。
    【解决方案2】:

    您需要以完全信任的方式运行您的 IIS 以获得反射权限, 看这里reflection permission

    或这里 set trust level

    you need this because Automapper uses reflection to emit code o_O
    

    【讨论】:

      【解决方案3】:

      取消阻止 dll(如 Lee 所说,右键单击 AutoMapper.dll > 选择属性 > 取消阻止。),然后只需回收站点的应用程序池。无需重启 IIS。

      【讨论】:

        猜你喜欢
        • 2011-04-04
        • 2010-09-06
        • 2023-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多