Powered by Blogger.

Sunday, February 23, 2014

Logging In Users Programmatically in Asp.Net



In this post we will discuss on how we can Logging in users programmatically in Asp.Net. Also you can check out my previous posts on:

- Sql Server 2008 joins tutorial

- Body onload function in content page in asp.net

- DataAdapter in Asp.Net

You can use pre-built mechanics of the Login control or you can also perform this task programmatically using the Membership class. To validate credentials that you receive, you use the ValidateUser() method of this class. The ValidateUser() method takes a single signature:

Membership.ValidateUser(username As String, password As String)

Validating a user’s credentials programmatically

In VB.net:

If Membership.ValidateUser(TextBox1.Text, TextBox2.Text) Then
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, False)
Else
Label1.Text = "You are not registered with the site."
End If

In C#.net:
if (Membership.ValidateUser(TextBox1.Text, TextBox2.Text)
{
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text.ToString(), false);
}
else
{
Label1.Text = "You are not registered with the site.";
 }

The ValidateUser() method returns a Boolean value of True if the user credentials pass the test and False if they do not.

you can see that end users whose credentials are verified as correct are redirected from the login page using the RedirectFromLoginPage() method. This method takes the username and a Boolean value that specifies whether the credentials are persisted through a cookie setting.



0 comments

Post a Comment