Something that people normally overlook when building a system is logging so here is a little snippet of code that will detect when an account has become locked out making it possible to notify either support staff or the end user that there current account has been locked out.
This following works by taking a reading of the user account before and after the login event. So we read the status of the account on the page postback. Then we test for it during the login error. We can then log this or send an email to the current user.
public partial class Login : AppPage
{
private bool AccountWasLocked = false;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
MembershipUser tmp = Membership.GetUser(Login2.UserName);
if (tmp != null)
AccountWasLocked = tmp.IsLockedOut;
}
}
protected void Login2_LoginError(object sender, EventArgs e)
{
MembershipUser tmp = Membership.GetUser(Login2.UserName);
if (tmp != null && AccountWasLocked == false && tmp.IsLockedOut == true)
{
string Msg = string.Format("User '{0}' Has Locked Out their Account from IP Address '{1}'", tmp.UserName, Request.UserHostAddress);
DBLog.Log(DBConn, tmp, "Login.LoginError.AccountLocked", Msg);
//Notify Admin / End User
}
DBLog.Log(DBConn, "Login.Failure", string.Format("Login failed for user '{0}' from {1}", Login2.UserName, Request.UserHostAddress));
}
protected void Login2_LoggedIn(object sender, EventArgs e)
{
DBLog.Log(DBConn, "Login", string.Format("User '{0}' has logged in from {1}", Login2.UserName, Request.UserHostAddress));
}
}
In the above code you will also need to include the namespace System.Web.Security to be able to access the MemberShip functions in asp.net.
The class DBLog that I am using has two static functions which you will also need to implement depending on how you wish to store your log information.
public class DBLog
{
public static void Log(DBConn Conn, MembershipUser User, string LogType, string Message);
public static void Log(DBConn Conn, string LogType, string Message);
}
I will probably be making some posts in the near future about logging in asp.net