PageParser.GetCompiledPageInstance抛出一个SecurityException - 如何解决这个问题?

时间:2011-05-01 07:57:10

标签: asp.net controller httphandler

背景:我创建了一个自定义HttpHandler,它根据用户发布的参数执行特定命令。由于JQuery Ajax在我的网站中被大量使用,我采用了内容页面方法,其中我执行包含用户正在查看的容器页面内容的aspx页面。 截至目前,我正在使用Godaddy共享主机,因为该网站还处于起步阶段,我无法使用专用/虚拟服务器。

我的电脑上的一切正常,但服务器上没有。 我收到了这个错误:

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed.]
   System.Web.UI.PageParser.GetCompiledPageInstance(String virtualPath, String inputFile, HttpContext context) +46
   SL.Controller.Commands.CommandHelper.ExecutePage(SLActionInfo actionInfo, String url) +95
   SL.Controller.Commands.ProductCommand.Execute(SLActionInfo actionInfo) +32
   SL.Controller.CommandFactory.ExecuteCommand(HttpContext context) +224
   SL.Controller.DefaultHttpHandler.ProcessRequest(HttpContext context) +20
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
....

奇怪的是:Server.Execute(string virtualUrl)正在运行,但PageParser.GetCompiledPageInstance却没有。

为什么我使用PageParser.GetCompiledPageInstance而不是Server.Execute(string url)?由于以下代码:

public static string ExecutePage(SLActionInfo actionInfo, string url)
        {
            var context = actionInfo.Context;
                        var sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            IHttpHandler handler = PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context);
            if (handler is SL.UI.SLPageBase)
                ((SL.UI.SLPageBase)handler).ActionInfo = actionInfo;
            context.Server.Execute(handler, htw, true);
            return sw.ToString();
        }

由于我的内容页面来自SLPageBase(Page的子类),它具有需要在此ExecutePage方法中设置的属性ActionInfo,我使用的是PageParser方法。

我不知道如何在不破坏我的PageParser方法的情况下摆脱这个错误。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我为上述问题设计了一个解决方法。似乎这个问题的读者没有遇到这个问题因此无法提出可能的解决方案。由于使用共享主机的人可能会遇到此问题并可能寻找解决方案,因此他们可以使用此解决方法。

编辑:我决定不立即接受我的回答。我期待着对这个答案的评论,并会在几天后相应地做出决定。

这打破了我的PageParser.GetCompiledPageInstance方法,但影响很小,我可以在完全信任环境中托管时轻松切换到原始方法。

这种变化分为两个方面:

1)在ExecutePage方法中:

public static string ExecutePage(SLActionInfo actionInfo, string url)
{
    var context = actionInfo.Context;
    var sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    // The original approach is commented out.
    // IHttpHandler handler = PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context);
    // if (handler is SL.UI.SLPageBase)
    // ((SL.UI.SLPageBase)handler).ActionInfo = actionInfo;
    // context.Server.Execute(handler, htw, true);

    // The new approach:
    // Add actionInfo to the Items collection so that any page executing in the context of this request can read it.
    Context.Items.Add("SLActionInfo", actionInfo);
    // Now execute the page by providing its url.
    context.Server.Execute(url, htw, true);

    return sw.ToString();
}

2)另一个更改是在SLPageBase.ActionInfo属性中:

public SLActionInfo ActionInfo
{
    get
    {
        return (SLActionInfo)Context.Items["SLActionInfo"];
        // Commented out the old approach.
        // return _actionInfo;
    }
}

如果我想恢复旧的(并且是首选的)方法,你可以看到它是多么容易。只需在这里取消注释几行并注释几行即可。系统的其余部分将保持不受影响。