RedirectFromLoginPageEx – Specify how many days the auth cookie lasts

' A custom routine that works like FormsAuthentication.RedirectFromLoginPage ' but lets you control the authentication cookie's expiration date' Example: create an auth cookie that lasts 7 days'  RedirectFromLoginPageEx(username, persistent, 7)Function RedirectFromLoginPageEx(ByVal username As String, _    ByVal persistentCookie As Boolean, Optional ByVal expirationDays As Integer _    = -1) As Boolean    ' Get the URL of the requested resource.    Dim url As String = FormsAuthentication.GetRedirectUrl(username, _        persistentCookie)    ' Create the authentication cookie.    ' (The cookie path can be omitted, because it defaults to "/",    '  the entire site.)    FormsAuthentication.SetAuthCookie(username, persistentCookie, "/")    If persistentCookie And expirationDays > 0 Then        ' Get a reference to the cookie just created.        Dim cookie As HttpCookie = Response.Cookies _            (FormsAuthentication.FormsCookieName)        ' Set its expiration date.        cookie.Expires = Now.AddDays(expirationDays)        ' Ensure the cookie can travel only over https.        'cookie.Secure = True    End If    ' Redirect to the resource that was requested originally.    Response.Redirect(url)End Function' Note: This code is taken from Francesco Balena's' "Programming Microsoft Visual Basic .NET" - MS Press 2002, ISBN 0735613753' You can read a free chapter of the book at ' http://www.vb2themax.com/HtmlDoc.asp?Table=Books&ID=101000

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: