Powered by Blogger.

Saturday, February 15, 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:

- ModalPopupExtender background css not coming when showing from code behind in asp.net

- Show confirmation dialogbox using JavaScript in Asp.Net

- Data access block of Microsoft enterprise library 6.0 using stored procedure

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