2009-09-27 48 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