Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: Intermediate
Dec 16, 2002

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
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date