Session access disabled for asp.net package requests
I am using asp.net and MVC4 for my application and also I am using package for css and js files.
When I look at the trace files, I noticed that all my requests are session related . that is, all package requests are passed through sessionstatemodule.
My footprint looks like below.
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
I guess I don't need session access for my package requests. How can I disable session access for my package requests?
source to share
If I understand your question correctly, you want to disable session state for your static resources. To do this, you can do two things:
1) Disable SessionState
forController
To do this, you need to import the namespace System.Web.SessionState
and then you decorate your controller with the following line of code:
[SessioState(SessionStateBehavior.Disabled)
public class HomeController: Controller
{
}
For more information, you can visit the following link:
Session state level management
2) Creating static resources
Setting up IIS:
Create two websites in the inetpub directory.
- www.domain.com // For main site
- static.domain.com // Foe Static Resources
Now point them to the same physical directory i.e.
C: \ Inetpub \ www.domain.com
Redirect domain.com to www.domain.com
Redirect any domain.com request to www.domain.com.
so that any request for domain.com will be redirected to www.domain.com because the cookie for domain.com will also be used by all sub-domains including static.domain.com, so these are very important steps
** Code changes **
Add below code to your web.config file:
<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>
use PreApplicationStartMethod
and Microsoft.Web.Infrastructure
to dynamically register the HTTP module at startup stage before the application
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()
{
}
}
Add extension method
public static class Extensions
{
public static string StaticContent(this UrlHelper url, string contentPath)
{
string strStaticDomain = ConfigurationManager.AppSettings["StaticDomain"];
return contentPath.Replace("~", strStaticDomain);
}
}
Now use @Url.StaticContent()
from view to render the static url of the resource with static.domain.com, be it an image, script, CSS or bindings or wherever we want to refer to the cookieless domain. eg,
<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="" />
Follow the link for full details as the article is quite long:
Invalid cookie domain for binding and static resources
Hope this helps you achieve your goal.
source to share