locked
SP2010 Session RRS feed

  • Question

  • What is sliding session ? When I try to use this I am unable to catch hold of session expiration ?

    What about ASP.Net Session ? How to use that ?

    My requirement is when session expires we need to redirect user to a page with a login page hyperlink.

     

    Thursday, February 2, 2012 2:57 AM

All replies

  • To enable session you will need to modify the web.config and restart IIS:

  • Uncomment the System.Web.SessionState.SessionStateModule http module to sharepoints web.config file.
  • Modify so that pages element appears as follows: <pages enableSessionState="true" /> to sharepoints web.config file.

    Here is an example sharepoint global.asax that uses session:

     

    <%@ Assembly Name="Microsoft.SharePoint"%>
    <%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
    <%@ Import Namespace="System.Web.Security" %>
    <%@ Import Namespace="System.Web.SessionState" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System" %>
    <script Language="C#" RunAt="server">

        protected void Application_BeginRequest(object source, EventArgs e)
        {
            SPHttpApplication spApplication = (SPHttpApplication)source;
            HttpContext context = spApplication.Context;
            string filePath = context.Request.FilePath;
            string fileExtension =
                VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".aspx"))
            {
                try
                {
                    string sessionId = Session["test"].ToString();
                    string cookieValue = Request.Cookies["test"].Value;
                    if (sessionId != cookieValue)
                    {
                        throw new Exception("Session expired.");
                    }
                }
                catch
                {
                    FormsAuthentication.SignOut();
                    if (Session != null)
                        Session.Abandon();
                    FormsAuthentication.RedirectToLoginPage();
                }
            }

        }


        protected void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            if (Session.IsNewSession)
            {
                Guid guid = Guid.NewGuid();
                if (Request.Cookies["test"] != null)
                {
                    Request.Cookies.Remove("test");
                }

                HttpCookie cookie = new HttpCookie("test", guid.ToString());
                Response.Cookies.Add(cookie);
                Session[D3CookieKey] = guid.ToString();
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {
            if (Request.Cookies[D3CookieKey] != null)
            {
                Request.Cookies.Remove("test");
            }
        }
    </script>


  • Allan Merolla | BEng, MPM, JD | MCP, MCTS, MCPD | SharePoint/SQLBI/.NET | My Blog at http://www.parallelfun.com/
Thursday, February 2, 2012 3:41 AM
  • Sometimes you need to also enable SessionState in IIS and not just your sharepoint application: 1.Open IIS 7 manager, and find your web application. 2.Double click "Modules" in the IIS section. 3.Click "Add Managed Module..." on the right hand pane. 4.In the Add Managed Module dialog, enter "SessionState" or something like that for the name, and choose the following item from the dropdown: System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
    Allan Merolla | BEng, MPM, JD | MCP, MCTS, MCPD | SharePoint/SQLBI/.NET | My Blog at http://www.parallelfun.com/
    Thursday, February 2, 2012 3:52 AM
  • To enable session you have to run a command  in the SharePoint 2010 Management Shell "Enable-SPSessionStateService –DefaultProvision"

    Also you have to turn on the enableSessionState property in web.config afterwards. Please refer the following link.

    http://blogs.msdn.com/b/markarend/archive/2010/05/27/using-session-state-in-sharepoint-2010.aspx

    If you want to automatically redirect the page when the session expires with out refreshing the page, you have to go for javascript code.

    Thursday, February 2, 2012 5:13 AM
  • Thanks for the suggestions. What I heard from different article is we cannot use global.asax for catching session end / expiry in SP 2010 as the product itself uses it. Please correct me if I am wrong. In that scenario where do i write the session end / expiry and redirect to the custom page.
    Thursday, February 2, 2012 5:44 PM
  • I am unable to get the session end event in global.asax
    Tuesday, February 7, 2012 12:42 PM