【问题标题】:Session access disable for asp.net bundle requests禁用 asp.net 捆绑请求的会话访问
【发布时间】:2015-09-09 00:33:33
【问题描述】:

我正在为我的应用程序使用 asp.net 和 MVC4,并且我正在为 css 和 js 文件使用捆绑。

当我查看我的跟踪文件时,我发现我所有的捆绑请求都在使用会话。即,所有捆绑请求都通过 sessionstatemodule。

我的跟踪如下所示。

155. NOTIFY_MODULE_START ModuleName="Session", Notification="REQUEST_ACQUIRE_STATE", fIsPostNotification="false" 06:59:43.480 
156. AspNetPipelineEnter Data1="System.Web.SessionState.SessionStateModule" 06:59:43.480 
157. AspNetSessionDataBegin  06:59:43.480 
158. AspNetSessionDataEnd  06:59:43.996 
159. AspNetPipelineLeave Data1="System.Web.SessionState.SessionStateModule" 06:59:43.996 
160. NOTIFY_MODULE_COMPLETION ModuleName="Session", Notification="REQUEST_ACQUIRE_STATE", fIsPostNotificationEvent="false", CompletionBytes="0", ErrorCode="The operation completed successfully. (0x0)" 06:59:43.996 
161. NOTIFY_MODULE_END ModuleName="Session", Notification="REQUEST_ACQUIRE_STATE", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE" 06:59:43.996 

我认为我的捆绑请求不需要会话访问权限。如何禁用我的捆绑请求的会话访问?

【问题讨论】:

    标签: asp.net .net asp.net-mvc bundling-and-minification


    【解决方案1】:

    如果我正确理解您的问题,您希望禁用静态资源的会话状态。为此,您可以做两件事:

    1) 为Controller禁用SessionState

    为此,您需要导入 System.Web.SessionState 命名空间,然后使用以下代码行装饰您的控制器:

    [SessioState(SessionStateBehavior.Disabled)
    public class HomeController: Controller
    {
    }
    

    欲了解更多信息,您可以访问以下链接:

    Controlling Session State Behavior

    2) 创建静态资源

    IIS 设置:

    在您的 inetpub 目录中创建两个网站。

    1. www.domain.com // 用于主站点
    2. static.domain.com // 敌人静态资源

    现在将它们指向相同的物理目录,即

    C:\inetpub\www.domain.com

    将 domain.com 重定向到 www.domain.com

    将任何 domain.com 请求重定向到 www.domain.com。

    因此必须将任何 domain.com 请求重定向到 www.domain.com 因为为 domain.com 设置的 cookie 也将由所有子共享 域包括 static.domain.com 因此这是非常重要的步骤

    ** 代码更改**

    在您的 web.config 文件中添加以下代码:

    <appSettings>
    
      <add key="StaticSiteName" value="static.domain.com"/>
    
      <add key="StaticDomain" value="http://static.domain.com"/>
    
      <add key="MainDomain" value="http://www.domain.com"/>
    
    </appSettings>
    

    在预应用启动阶段使用PreApplicationStartMethodMicrosoft.Web.Infrastructure动态注册HTTP模块

    public class PreApplicationStart
    
    {
    
        public static void Start()
    
        {
    
            string strStaticSiteName = ConfigurationManager.AppSettings["StaticSiteName"];
    
            string strCurrentSiteName = HostingEnvironment.SiteName;
    
    
    
            if (strCurrentSiteName.ToLower() == strStaticSiteName.ToLower())
    
            {
    
                DynamicModuleUtility.RegisterModule(typeof(StaticResource));
    
            }
    
        }
    
    }
    
    
    
    public class StaticResource : IHttpModule
    
    {
    
        public void Init(HttpApplication context)
    
        {
    
            context.BeginRequest += new EventHandler(context_BeginRequest);
    
        }
    
    
    
        void context_BeginRequest(object sender, EventArgs e)
    
        {
    
            HttpContext context = HttpContext.Current;
    
            string strUrl = context.Request.Url.OriginalString.ToLower();
    
    
    
            //HERE WE CAN CHECK IF REQUESTED URL IS FOR STATIC RESOURCE OR NOT
    
            if (strUrl.Contains("Path/To/Static-Bundle/Resource") == false)            
    
            {
    
                string strMainDomain = ConfigurationManager.AppSettings["MainDomain"];
    
                context.Response.Redirect(strMainDomain);
    
            }
    
        }
    
    
    
        public void Dispose()
    
        {
    
        }
    

    }

    添加扩展方法

    public static class Extensions
    
    {
    
        public static string StaticContent(this UrlHelper url, string contentPath)
    
        {
    
            string strStaticDomain = ConfigurationManager.AppSettings["StaticDomain"];
    
            return contentPath.Replace("~", strStaticDomain);
    
        }
    
    }
    

    现在从视图中使用@Url.StaticContent(),这样无论是图像、脚本、CSS 或捆绑包,还是我们想要引用无cookie 域的任何地方,它都会使用static.domain.com 呈现静态资源url。例如

    <link href="@Url.StaticContent("~/Content/Site.css")" rel="Stylesheet" />
    
    <script src="@Url.StaticContent("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
    
    <script src="@Url.StaticContent("~/bundles/jquery")" type="text/javascript"></script>
    
    
    
    <img src="@Url.StaticContent("~/Images/heroAccent.png")" alt="" />
    

    由于文章很大,请访问以下链接以获取完整信息:

    Cookie less domain for bundling and static resources

    希望这将帮助您实现目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2016-05-17
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多