Tuesday, 19 April 2011

How to Implement Form Authentication in asp.net 2005

In web config File :
<authentication mode="Forms">
   <forms name="test" loginUrl="~/Login.aspx" path="/" timeout="25" protection="All" ></forms>
  </authentication>

  <!--<authorization>
   <deny users="?" />
  <allow users="*" />
  </authorization>-->
   or
    <authorization>
      <allow roles="Admin"/>
      <deny users="*"/>
    </authorization>




protected void btnSubmit_Click(object sender, EventArgs e)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUserName.Text.Trim(), DateTime.Now,
DateTime.Now.AddMinutes(25), false, strRole);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
//FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); if using doesnt work
Response.Redirect("BackOfficeOption.aspx");
}

No comments:

Post a Comment