Tuesday, November 6, 2012

Form Authentication


<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="userDbConnectionString" connectionString="Data Source=localhost;Initial Catalog=sdsdfsdf;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH" protection="All">
      </forms>
    </authentication>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
  <location path="admin">
    <system.web>
      <authorization>
        <allow roles="admin"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="reseller">
    <system.web>
      <authorization>
        <allow roles="reseller"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="customer">
    <system.web>
      <authorization>
        <allow roles="customer"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
 </configuration>




protected void Button1_Click(object sender, EventArgs e)
    {
        user _user = new user();
        adminManager _adminManager = new adminManager();
        DataTable dt = new DataTable();
        dt = _adminManager.fetchuserDetails(TextBox1.Text);
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["password"].ToString() == TextBox2.Text)
            {
                FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(
                                                        1,
                                                        dt.Rows[0]["emailID"].ToString(),
                                                        DateTime.Now,
                                                        DateTime.Now.AddMinutes(30),
                                                        CheckBox1.Checked,
                                                        dt.Rows[0]["role"].ToString(),
                                                        FormsAuthentication.FormsCookiePath);

                string hash = FormsAuthentication.Encrypt(Authticket);

                HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

                if (Authticket.IsPersistent) Authcookie.Expires = Authticket.Expiration;

                Response.Cookies.Add(Authcookie);

                string returnUrl = Request.QueryString["ReturnUrl"];
                if (returnUrl == null) returnUrl = "/";

                Response.Redirect(returnUrl);
            }
            else
            {
                Label1.Text = "Password does'nt match.";
            }
        }
        else
        {
            Label1.Text = "User not exists.";
        }
    }



protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated && Request.QueryString["ReturnUrl"] != null)
        {
            
        }

        Label1.Text = "";
    }

No comments:

Post a Comment