ASP.NET 2.0 Membership

Today I’ve been spending a bit of time fiddling around with Visual Studio 2005, particularly the ASP.NET 2.0 membership functionality. When I visited 4GuysFromRolla.com to see what they had to say about it, I came across 5-part series of articles. The examples are in VB.NET, so I’ve been rewriting them in C# (my preferred .NET language) as I go along. What stood out about their information message example in part 4 of the series was that they added a label to their login page and set the text string based on the error details.

It seemed to me that there should be a property in the Login control that could be set programmatically instead of adding a label that results in two error messages for the user to look at. It didn’t take much digging before I found it. The property: FailureText.

Here’s how my revised code looks:

protected void Login1_LoginError(object sender, EventArgs e){

MembershipUser userInfo = Membership.GetUser(this.Login1.UserName);

if (userInfo == null){

//The user entered an invalid username...

SetLoginFailureText(string.Format("No account exists with the username '{0}'.",this.Login1.UserName));

} else {

if (!userInfo.IsApproved){

SetLoginFailureText(string.Format("The account with the username '{0}' has not yet been approved by the site's administrators.", this.Login1.UserName));

} else {

if (userInfo.IsLockedOut){

SetLoginFailureText(string.Format("The account with the username '{0}' has been locked out.", this.Login1.UserName));

}

}

}

}

private void SetLoginFailureText(string failureText){ this.Login1.FailureText = failureText; }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.