NotificationSubscribers - Part 4 - Overriding the Login Expiration Period for Extranet Auto Login
In Dynamicweb 7.1, a new feature was introduced that enables users to stay logged in between browser sessions.The default expiration period for the cookie that is set to enable auto-login is 30 days. As far as I know, there's no configuration option to change that period. However, with a simple NotificationSubscriber it's easy to change it yourself.
Enabling Auto Login
To enable auto-login, you need a login template that has fields for the user name and password, two checkboxes to remember the user name and password, and a checkbox for the auto-login feature. A sample template could look like this:
<form name="ExtUserForm" method="post" action="<!--@DWExtranetAction-->"> <label for="Username">Username</label> <input type="text" name="Username" value="<!--@DWExtranetUsername-->" /><br /> <label for="Password">Password</label> <input type="password" name="Password" value="<!--@DWExtranetPassword-->" /><br /> <input type="checkbox" value="True" name="DWExtranetUsernameRemember" <!--@DWExtranetUsernameRemember--> /> <label for="DWExtranetUsernameRemember"> Remember username</label><br /> <input type="checkbox" value="True" name="DWExtranetPasswordRemember" <!--@DWExtranetPasswordRemember--> /> <label for="DWExtranetPasswordRemember"> Remember password</label><br /> <input type="checkbox" value="True" name="autologin"> <label for="autologin"> Autologin</label><br /> <input type="Submit" value="Login"> </form>
With this setup, a user can login using the Extranet module. When all three check boxes are checked, Dynamicweb creates a cookie with a lifetime of 30 days to remember the user's details. The user is logged in automatically the next time she visits the web site within 30 days.
Modifying the Lifetime of the Cookie
To override the period for which the cookie that is set is valid, you can create a NotificationSubscriber that subscribes to the OnExtranetLogin event, and then change the Expires property of the DW_Extranet cookie like this:
[Subscribe(Standard.User.OnExtranetLogin)]
public class ExtranetLoginSubscriber2 : NotificationSubscriber
{
public override void OnNotify(string notification, NotificationArgs args)
{
System.Web.HttpContext.Current.Response.Cookies["DW_Extranet"].Expires = DateTime.Now.AddDays(10);
}
}
This code is fired when the user logs in, either manually or when the auto-login feature kicks in. This way, the cookie is extended with another 10 days every time the user visits the web site.
You can read more about the auto-login feature in the release notes for Dynamicweb 7.1.