Usable Asp.Net

Little things that make Asp.Net more usable

Reporting Errors in Login Control

Login control has a FailureText property that holds the text to display to the user when a login attempt fails.

What if you want to report errors before making an login attempt? For example, you may handle the LoggingIn event to check a "IsDeleted" flag on user record and, if it is set, provide an error "The user is inactive". How can you set the error text without making an login attempt?

This code will do that:

protected void LoggingIn(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)

{

string userName = this.Login1.UserName;

// validate the user

if (IsUserDeleted(userName))

{

// deleted, cancel login

e.Cancel = true;

// notify the user

(this.Login1.FindControl("FailureText") as System.Web.UI.WebControls.Literal).Text = String.Format("The user {0} is inactive", userName);

}

}

 

Leave a Comment

(required) 

(required) 

(optional)

(required)