2009-09-27 37 views
2

我想要得到一个ASP.NET MVC应用程序工作...我应该知道这并不容易。前几页工作,但它们都是静态的。第一次执行一个控制器时,我会得到下面的例外。ASP.NET MVC应用程序有SecurityException

这里是控制器的操作方法:

[AcceptVerbs(HttpVerbs.Get)] 
public ActionResult Index(Section? section, int? parent) 
{ 
    if (section == null) 
    { 
     return RedirectToAction("Index", "Questions", new {section = Section.Section0}); 
    } 

    IPagedList<Question> questions = _surveyService.FetchQuestions(User.Identity.Name, section.Value, parent); 

    // ... 

    ViewResult result = View("Index", questions); 
    result.ViewData.Add("CurrentSection", section.Value); 
    result.ViewData.Add("Parent", parent); 
    result.ViewData.Add("IsLastPage", questions.IsLastPage); 

    return result; 
} 

该异常是在RedirectToAction()抛出的方法中的第二行。

背景:

  • 我跟着this answer的说明。
  • 我没有在代码中明确使用反射或要求安全。
  • 我使用MVC的LINQ to SQLElmahPagedList
  • 我正在使用IIS 7集成模式。
  • 我加入[组件: AllowPartiallyTrustedCallers]到我 AssemblyInfo.cs中。我这样做是因为我发现另一个堆栈溢出问题,它有一个建议它的答案(我现在找不到它,否则我会提供一个链接)。我也强烈地按照Rex M的回答下面的建议命名我的程序集。

我缺少什么,使这项工作?

例外:

Server Error in '/surveys/objectification' Application. 
    Security Exception 
    Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. 

    Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers. 

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

    Stack Trace: 

    [SecurityException: That assembly does not allow partially trusted callers.] 
     SelfObjectificationSurvey.Web.Controllers.QuestionsController.Index(Nullable`1 section, Nullable`1 parent) +0 
     lambda_method(ExecutionScope , ControllerBase , Object[]) +123 
     System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178 
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24 
     System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() 
+53 
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258 
     System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() 
+20 
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193 
     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) 
+382 
     System.Web.Mvc.Controller.ExecuteCore() 
+123 
     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23 
     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7 
     System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +144 
     System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54 
     System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7 
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
+181 
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
+75 


    Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.4049 
+0

我很好奇,一个接受答案的7岁老人的问题是如何得到解决的? – jrummell 2016-08-21 16:49:54

回答

2

的另一件事是,根据本article,有不能在部分受信任的装置中可以使用的一些.NET类型,即使它已被装饰AllowPartiallyTrustedCallersAttribute程序。

查看.NET Framework Assemblies and the AllowPartiallyTrustedCallers Attribute获取完整列表。

更新2 您确定您调用的所有第三方程序集也都使用AllowPartiallyTrustedCallers属性进行了装饰吗?

例如,看看PagedList 1.1的AssemblyInfo.cs它似乎不包含此属性。

更新1:你是对的,那些不可用类型的列表看起来非常过时。

LINQ to SQL FAQ有关于在部分信任环境中的使用一些有趣的信息:

APTCA

问:是否将System.Data.Linq标志着由部分信任的代码使用 ?

A.是System.Data.Linq.dll 组件标有 AllowPartiallyTrustedCallersAttribute程序 属性那些.NET框架 组件之间。如果没有此标记,则.NET Framework中的程序集 是 ,只能由完全可信的 代码使用。

在LINQ主要场景到SQL 用于允许部分可信赖呼叫者 是使LINQ到SQL组件 从Web应用程序访问, 其中信任配置是 平台。

+0

这是一个旧列表(.NET 1.1),我希望System.Data.SqlClient不在其上。 – jrummell 2009-09-27 21:59:44

+0

到目前为止,我已经删除了elmah,但我仍然有例外,PagedList是... – jrummell 2009-09-27 23:48:01

+0

PagedList是问题,谢谢!我用AllowPartiallyTrustedCallers属性重新编译它,它工作。我会在codeplex上记录这个问题,以防其他人被绊倒。 – jrummell 2009-09-28 00:00:31

2

是你的组件strong-named

AllowPartiallyTrustedCallersAttribute仅在汇编级别的强命名程序集中应用时才有效。您可能要检查

+0

不,他们不是。我会试一试。 – jrummell 2009-09-27 20:52:28

+0

我添加了一个强名称密钥,重新编译,上传并再次尝试。它给出了相同的例外=/ – jrummell 2009-09-27 21:07:01

1

您可能需要完全信任模式来运行您的代码。大多数主机只允许中等信任,就像GoDaddy一样。您可能不得不将主机切换到另一个能够给予您完全信任的主机。

尽管MVC本身不应该要求比中等信任更多,但是您的其他代码可能会有。只需在代码中的某个地方进行运行时类型检查即可,以便制定反思,反过来又希望得到完全的信任。

+0

根据这个答案,http://stackoverflow.com/questions/266205/is-there-a-way-that-i-can-run-a-asp-net-mvc-project-on-godaddy-com- shared-web-hos/299339#299339,你可以在GoDaddy中托管ASP.NET MVC。我添加的唯一东西是LINQ to SQL和PagedList。尽管我讨厌GoDaddy,但我不希望在明年账户到期之前切换。 – jrummell 2009-09-27 21:58:22

0

LINQ to SQL可能会出现问题 - LINQ to SQL通常会生成一个存储过程。如果你的代码试图在中等信任的情况下这样做,它可能会导致APTCA异常。

+0

LINQ to SQL不会生成存储过程...至少不在我的项目中。 – jrummell 2009-09-27 22:42:12

2

一个接一个退出你的程序集,看看谁是罪魁祸首。无需猜测。我在Microsoft Enterprise Libraries中遇到了这个问题。

+0

到目前为止,我已经删除了elmah,我仍然有例外,PagedList是... – jrummell 2009-09-27 23:55:18

+0

看到你已经解决了,很高兴这有帮助。 – 2009-09-28 00:14:46

+0

它做到了!如果可以接受多个答案,我也会接受这个答案。 – jrummell 2009-09-28 03:44:04