Link to home
Start Free TrialLog in
Avatar of macros14
macros14

asked on

Forms Authentication without redirect

I have authentication (username and password textboxes) on my homepage.  The user enters there username and password, and everything works correctly when I have this statement after I build my authentication ticket and cookie.

response.Redirect(FormsAuthentication.GetRedirectUrl(sUserName, False), False)

But since this isn't a login page but rather a homepage, I don't want to redirect.  If I comment out the response.redirect, and then ask If User.Identity.IsAuthenticated it says the user isn't.  Is there a way to get around having to redirect to get the user added to the authentication list??

Here is more of my authentication method

' Initialize FormsAuthentication (reads the configuration and gets
                ' the cookie values and encryption keys for the given application)
                FormsAuthentication.Initialize()

                If cbKeepLoggedIn.Checked Then
                    bKeepLoggedIn = True
                    dLogoutDate = Now.AddDays(8)
                End If

                ' Create a new ticket used for authentication
                Dim Ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, sUserName, _
                                              Now, dLogoutDate, bKeepLoggedIn, dt.Rows(0)("ROLE_CD").ToString, FormsAuthentication.FormsCookiePath)

                ' Hash the cookie for transport over the wire
                Dim hash As String = FormsAuthentication.Encrypt(Ticket)
                Dim cookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, hash)

                If (Ticket.IsPersistent) Then
                    cookie.Expires = Ticket.Expiration
                End If

                ' Add the cookie to the list for outbound response
                response.Cookies.Add(cookie)

                'response.Redirect(FormsAuthentication.GetRedirectUrl(sUserName, False), False)
Avatar of mcgants
mcgants

If you just leave the response.redirect off, you can set the redirect URL, but not use it!
I have done this in one of my projects, it leaves the user logged in, but doesn't take them anywhere:

URL = FormsAuthentication.GetRedirectUrl(sUserName, False)

I'd recommend letting the user know they've logged in, but it should be as simple as that.

Cheers,
mcg
Avatar of macros14

ASKER

Doesn't work, I do like you say

FormsAuthentication.Initialize()

                If cbKeepLoggedIn.Checked Then
                    bKeepLoggedIn = True
                    dLogoutDate = Now.AddDays(8)
                End If

                ' Create a new ticket used for authentication
                Dim Ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, sUserName, _
                                              Now, dLogoutDate, bKeepLoggedIn, dt.Rows(0)("ROLE_CD").ToString, FormsAuthentication.FormsCookiePath)

                ' Hash the cookie for transport over the wire
                Dim hash As String = FormsAuthentication.Encrypt(Ticket)
                Dim cookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, hash)

                If (Ticket.IsPersistent) Then
                    cookie.Expires = Ticket.Expiration
                End If

                ' Add the cookie to the list for outbound response
                response.Cookies.Add(cookie)

                Dim sURL As String = FormsAuthentication.GetRedirectUrl(sUserName, False)




The next thing I do is

If User.Identity.IsAuthenticated Then
                BuildAuthenticatedBox()
            End If

but it won't make it into the if statement because it says the isAuthenticated=false
I have found this formsauthentication.setauthcookie()

Which is suppose to do the same thing as redirect(authenticate the user) but it won't accept my created cookie but rather wants to build a new cookie which isn't what I want.
ASKER CERTIFIED SOLUTION
Avatar of mcgants
mcgants

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial