devxlogo

RedirectFromLoginPageEx – Specify how many days the auth cookie lasts

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

See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist