I'm trying do deny a specific user to login in the administrative area of the system, after it gets a FormsAuthenticationTicket with expiration of 30 days from now. I'm doing all manually and I'm using asp.net webforms.
My login code is as follows:
protected void btnLogin_Click(object sender, EventArgs e)
{
User u = LoginDataAccess.CheckLogin(txtEmail.Text, txtPassword.Text);
if (u.Id == 0)
{
lbInfo.Text = "Invalid credentials.";
lbInfo.CssClass = "label-warning";
}
else
{
LoginDataAccess.Authenticate(u, Response.Cookies, cbRememberMe.Checked);
}
}
And the LoginDataAccess.Authenticate method is this:
public static void Authenticate(User user, HttpCookieCollection cookies, bool remember)
{
GenericIdentity gi = new GenericIdentity(user.Name);
string role = UserRoles.GetRole(user.Roles);
GenericPrincipal gp = new GenericPrincipal(gi, new string[] { role });
FormsAuthentication.RedirectFromLoginPage(user.Name, true);
if (remember)
{
cookies.Clear();
DateTime expiryDate = DateTime.Now.AddDays(30);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, user.Nome, DateTime.Now, expiryDate, true, String.Empty);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authenticationCookie.Expires = ticket.Expiration;
cookies.Add(authenticationCookie);
}
}
My check login method search into the database for the user. It's clear for me that I need to do this every time that a user starts a session. How to do this?