Sitecore setting Timeout on a Virtual User


A customer was having issues getting a virtual users expiration to timeout. In looking at the code in the Authentication Manager I found that the method being used to load the virtual user passes expires as false and didn’t have an override.

To fix it I overwrote the Login(User) method of the  Authentication Provider and passed in the true variable.

Here’s my Authentication Provider.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Diagnostics;
using System.Web.Security;
using Sitecore.SecurityModel;
using Sitecore.Configuration;
using Sitecore.Security.Accounts;

namespace AdventureWorksMembership
{
    public class FormsAuthenticationProvider : Sitecore.Security.Authentication.FormsAuthenticationProvider
    {
        public override bool Login(Sitecore.Security.Accounts.User user)
        {
            Assert.ArgumentNotNull(user, "user");
            if (!base.Login(user))
            {
                return false;
            }
            this.StoreMetaData(user);
            FormsAuthentication.SetAuthCookie(user.Name, true);
            return true;
        }

        private void StoreMetaData(Sitecore.Security.Accounts.User user)
        {
            UserRuntimeSettings runtimeSettings = user.RuntimeSettings;
            if (runtimeSettings.IsVirtual)
            {
                ClientContext.SetValue("SC_USR_" + user.Name, runtimeSettings.Serialize());
            }
        }
    }
}

And in my web.config file

<!-- AUTHENTICATION -->
<authentication defaultProvider="forms">
<providers>
<clear/>
<add name="forms" type="AdventureWorksMembership.FormsAuthenticationProvider, AdventureWorksMembership"/>
</providers>
</authentication>

<authentication mode="None">
<forms name=".ASPXAUTH" cookieless="UseCookies" timeout="20"/>
</authentication>

It should be noted that setting slidingexpiration to true doesn’t work so this could open a whole new set of worms for you.

I have a fix for that here in this forum thread : http://sdn.sitecore.net/SDN5/Forum/ShowPost.aspx?pageindex=2&postid=21977

Advertisements

About Kevin Buckley
.Net web developer with a lot of experience in CMS. Currently working at Sitecore as Solutions Engineer.

2 Responses to Sitecore setting Timeout on a Virtual User

  1. Mark Ursino says:

    Great solution! Thanks for posting

  2. Pingback: Single Sign On for Sitecore CMS websites using Virtual Users. « horizontalintegration

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: