ASP.Net Membership: Determine whether the login failed is due to Unapproved or Locked our status
 
Author:
Filed Under: ASP.Net
Published Date: Jan 22, 2012
Views: 360
 

Abstract: Here Mudassar Ahmed Khan has explained how to determine that the login failure is due to invalid credentials, unapproved account or locked out account.

Comments:  0

 

In this article I will explain how to determine whether the login failed is due to locked out or not approved account.
ASP.Net Membership is a great library to build login and roles mechanism for ASP.Net Websites, but its ValidateUser method returns false in all the three cases
1. Invalid username or password.
2. Account is not approved or activated i.e. IsApproved is false
3. Account is suspended or disabled or locked i.e. IsLockedOut is false.
Thus it becomes difficult to find the exact issue and in many cases we need to display different messages based on different status. Hence I came up with the following solution
C#
bool isValid = Membership.ValidateUser(txtUserName.Text, txtPassword.Text);
if (!isValid)
{
    MembershipUser user = Membership.GetUser(txtUserName.Text);
    if (user != null)
    {
        //User exists
        if (!user.IsApproved)
        {
            //Account Unapproved
            lblMessage.Text = "Your account is not approved.";
        }
        else if (user.IsLockedOut)
        {
            //Account Locked
            lblMessage.Text = "Your account is locked.";
        }
        else
        {
            //Invalid username or password
            lblMessage.Text = "Invalid username or password.";
        }
    }
    else
    {
        //Invalid username or password
        lblMessage.Text = "Invalid username or password.";
    }
}
 
VB.Net
Dim isValid As Boolean = Membership.ValidateUser(txtUserName.Text, txtPassword.Text)
If Not isValid Then
     Dim user As MembershipUser = Membership.GetUser(txtUserName.Text)
     If user IsNot Nothing Then
         'User exists
         If Not user.IsApproved Then
             'Account Unapproved
             lblMessage.Text = "Your account is not approved."
         ElseIf user.IsLockedOut Then
             'Account Locked
             lblMessage.Text = "Your account is locked."
         Else
             'Invalid username or password
             lblMessage.Text = "Invalid username or password."
         End If
     Else
         'Invalid username or password
         lblMessage.Text = "Invalid username or password."
     End If
End If
 
Thus in the above way you can easily identify the exact reason for login failure in ASP.Net Membership library.








Related Articles



Comments

No comments have been added to this article.

Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit