Anda di halaman 1dari 5

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.Security;

public partial class login : System.Web.UI.Page


{
public static string constring =
WebConfigurationManager.ConnectionStrings["abit"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);

SqlCommand cmd = new SqlCommand("select uName,roles from users where


uName=@uName and pwd=@pwd", con);
cmd.CommandType = CommandType.Text;
string user=TextBox1.Text.ToUpper();
string pass=TextBox2.Text.ToUpper();
cmd.Parameters.AddWithValue("@uName", user);
cmd.Parameters.AddWithValue("@pwd", pass);

SqlDataReader dr;
if (cmd.Connection.State == ConnectionState.Open)
{
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["uName"] = user;
string roles = dr[1].ToString();

// Create forms authentication ticket


FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
user,// Username to be associated with this ticket
DateTime.Now, // Date/time ticket was issued
DateTime.Now.AddMinutes(5), // Date and time the cookie will expire
false, // if user has chcked rememebr me then create persistent cookie
roles, // store the user data, in this case roles of the user

FormsAuthentication.FormsCookiePath); // Cookie path specified in the


web.config file in <Forms> tag if any.

// To give more security it is suggested to hash it


string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new
HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket

// Add the cookie to the response, user browser


Response.Cookies.Add(cookie);

cmd.Parameters.Clear();
dr.Close();
//// Get the requested page from the url
//string returnUrl = Request.QueryString["ReturnUrl"];
//// check if it exists, if not then redirect to default page
//if (returnUrl == null) returnUrl = "admin\\Default.aspx?Uname=" + user;

Response.Redirect("admin\\Default.aspx");

//Response.Redirect("admin\\Default.aspx?Uname="+user);
}
else
{
string script = "alert(\"Wrong Credentials, Try Again !\");";

ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}
}
else
{
con.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
string roles = dr[1].ToString();

// Create forms authentication ticket


FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
user,// Username to be associated with this ticket
DateTime.Now, // Date/time ticket was issued
DateTime.Now.AddMinutes(5), // Date and time the cookie will expire
false, // if user has chcked rememebr me then create persistent cookie
roles, // store the user data, in this case roles of the user
FormsAuthentication.FormsCookiePath); // Cookie path specified in the
web.config file in <Forms> tag if any.

// To give more security it is suggested to hash it


string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new
HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket

// Add the cookie to the response, user browser


Response.Cookies.Add(cookie);

cmd.Parameters.Clear();
dr.Close();
//// Get the requested page from the url
//string returnUrl = Request.QueryString["ReturnUrl"];
//// check if it exists, if not then redirect to default page
//if (returnUrl == null) returnUrl = "admin\\Default.aspx?Uname=" + user;
Response.Redirect("Admin\\Default.aspx?Uname=" + user);

}
else
{
string script = "alert(\"Wrong Credentials, Try Again !\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}
}
}
}

Anda mungkin juga menyukai