forms authentication

M

mike parr

I'm using forms authentication and on my first page I want to check if
the user has a cookie, and if they do I want to authenticate then and
send them to my default page with a greeting, otherwise send them to a
login page.

I've seen a lot of examples on the internet where the user login details
are checked and then RedirectFromLoginPage is used, but I haven't seen
anything on simply authenticating the user and then sending them to a
page of your choosing.
I can check the user is valid against my database and then redirect
them, but they are not authenticated by the application and still can't
view my pages that require authentication.

Can anybody tell me how this is done?


Thanks,

Mike
 
C

Cowboy

All of this is automagic with .NET. To check for an auth cookie, you need to
make it persist (if you only want them to log in once and then come back to
the site without cookies).

Here is a C# snippet:

private void btnLoginSubmit_Click(object sender, System.EventArgs e)
{
string name = txtLoginUserNumber.Text;
string password = txtLoginPassword.Text;

if(IsUserAuthenticated(name, password)
{
//This persists the cookie across sessions
//Change to false to have user login each time
FormsAuthentication.RedirectFromLoginPage(name, true);
}
}

private bool IsUserAuthenticated(string name, string password)
{
//COde here to contact database
//Return true if authenticated
//False if not


//Initially hardcoded to true
return true;
}

VB.NET
--------
Private Sub btnLoginSubmit_Click(ByRef sender As Object, ByRef e As
System.EventArgs)

If (IsUserAuthenticated(name, password) Then
FormsAuthentication.RedirectFromLoginPage(name, true)
End If

End Sub

Private Function IsUserAuthenticated(ByVal name As String, _
ByVal password As String) As Boolean

'COde here to contact database
'Return true if authenticated
'False if not

Return True

End Function


Hope this helps. The help file is very good on this one, BTW.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
M

mike parr

When the user has already registered and has a persistent cookie on
their machine, how do you test this condition to know whether to send
them to your login page or your logged in page when they return to your
website?


Thanks,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top