none
Sharepoint 2013 - Clear cookie on sign out RRS feed

  • Question

  • I have created a cookie in javascript for some functionality in SP2013 master page . I want to clear that cookie when user click on sign out button(client side). I don't want to create custom signout page.

    Please let me know how can I achieve that?

    Wednesday, May 14, 2014 10:46 AM

Answers

  • Try below

    http://stackoverflow.com/questions/22381495/in-sharepoint-can-you-run-a-script-when-the-user-logs-out

    private void RemoveCookiesAndSignOut()
    {
        // Clear session state. 
        if (Context.Session != null)
        {
            Context.Session.Clear();
        }
    
        string cookieValue = string.Empty;
        if (Context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
            cookieValue = "NoCookie";
    
        // Clear my own cookie.
        HttpCookie cookieWinSignIn = Context.Request.Cookies["Morpheus_WindowsSignedIn"];
        if (cookieWinSignIn != null)
        {
            cookieWinSignIn.Value = cookieValue;
            Context.Response.Cookies.Remove("Morpheus_WindowsSignedIn");
            Context.Response.Cookies.Add(cookieWinSignIn);
        }
    
        // Remove cookies for authentication. 
        HttpCookie cookieSession = Context.Request.Cookies["WSS_KeepSessionAuthenticated"];
        if (cookieSession != null)
        {
            cookieSession.Value = cookieValue;
            Context.Response.Cookies.Remove("WSS_KeepSessionAuthenticated");
            Context.Response.Cookies.Add(cookieSession);
        }
    
        HttpCookie cookiePersist = Context.Request.Cookies["MSOWebPartPage_AnonymousAccessCookie"];
        if (cookiePersist != null)
        {
            cookiePersist.Value = cookieValue;
            cookiePersist.Expires = new DateTime(1970, 1, 1);
            Context.Response.Cookies.Remove("MSOWebPartPage_AnonymousAccessCookie");
            Context.Response.Cookies.Add(cookiePersist);
        }
    
        // Sign out.
        Microsoft.IdentityModel.Web.FederatedAuthentication.SessionAuthenticationModule.SignOut();
    }

     Or 

    jQuery(function ($) { var removedLinks = 'removedLinks' ,Options = { expires: 7, path: '/news' }, c=$.cookie(removedLinks)||'#DebuggingCode' $(c).remove(); $(".NewsHider a").click(function (e) { e.preventDefault(); var LinkClass = '.'+ $(this).attr('id'), removeLinksClasses=c.split(',') $(LinkClass).remove() removeLinksClasses.push(LinkClass) c=removeLinksClasses.join(',') $.cookie(removedLinks, c, Options) }); $('#NewsResetCookie').click(function(){ $.cookie(removedLinks,'',{expires:-1,path:'/news'}) }) });

    http://chrisstahl.wordpress.com/2012/08/31/use-jquery-cookies-in-sharepoint-2010/

    • Marked as answer by JasonGuo Sunday, May 25, 2014 7:12 AM
    Wednesday, May 14, 2014 11:55 AM
    Moderator
  • To do this, edit the file at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\signout.aspx

    Modify function _spBodyOnLoad()

    Add code to clear cookie before window.close().


    John Architect

    • Marked as answer by JasonGuo Sunday, May 25, 2014 7:12 AM
    Wednesday, May 14, 2014 12:21 PM

All replies

  • Try below

    http://stackoverflow.com/questions/22381495/in-sharepoint-can-you-run-a-script-when-the-user-logs-out

    private void RemoveCookiesAndSignOut()
    {
        // Clear session state. 
        if (Context.Session != null)
        {
            Context.Session.Clear();
        }
    
        string cookieValue = string.Empty;
        if (Context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
            cookieValue = "NoCookie";
    
        // Clear my own cookie.
        HttpCookie cookieWinSignIn = Context.Request.Cookies["Morpheus_WindowsSignedIn"];
        if (cookieWinSignIn != null)
        {
            cookieWinSignIn.Value = cookieValue;
            Context.Response.Cookies.Remove("Morpheus_WindowsSignedIn");
            Context.Response.Cookies.Add(cookieWinSignIn);
        }
    
        // Remove cookies for authentication. 
        HttpCookie cookieSession = Context.Request.Cookies["WSS_KeepSessionAuthenticated"];
        if (cookieSession != null)
        {
            cookieSession.Value = cookieValue;
            Context.Response.Cookies.Remove("WSS_KeepSessionAuthenticated");
            Context.Response.Cookies.Add(cookieSession);
        }
    
        HttpCookie cookiePersist = Context.Request.Cookies["MSOWebPartPage_AnonymousAccessCookie"];
        if (cookiePersist != null)
        {
            cookiePersist.Value = cookieValue;
            cookiePersist.Expires = new DateTime(1970, 1, 1);
            Context.Response.Cookies.Remove("MSOWebPartPage_AnonymousAccessCookie");
            Context.Response.Cookies.Add(cookiePersist);
        }
    
        // Sign out.
        Microsoft.IdentityModel.Web.FederatedAuthentication.SessionAuthenticationModule.SignOut();
    }

     Or 

    jQuery(function ($) { var removedLinks = 'removedLinks' ,Options = { expires: 7, path: '/news' }, c=$.cookie(removedLinks)||'#DebuggingCode' $(c).remove(); $(".NewsHider a").click(function (e) { e.preventDefault(); var LinkClass = '.'+ $(this).attr('id'), removeLinksClasses=c.split(',') $(LinkClass).remove() removeLinksClasses.push(LinkClass) c=removeLinksClasses.join(',') $.cookie(removedLinks, c, Options) }); $('#NewsResetCookie').click(function(){ $.cookie(removedLinks,'',{expires:-1,path:'/news'}) }) });

    http://chrisstahl.wordpress.com/2012/08/31/use-jquery-cookies-in-sharepoint-2010/

    • Marked as answer by JasonGuo Sunday, May 25, 2014 7:12 AM
    Wednesday, May 14, 2014 11:55 AM
    Moderator
  • To do this, edit the file at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\signout.aspx

    Modify function _spBodyOnLoad()

    Add code to clear cookie before window.close().


    John Architect

    • Marked as answer by JasonGuo Sunday, May 25, 2014 7:12 AM
    Wednesday, May 14, 2014 12:21 PM