kurdistan132
سه شنبه 01 شهریور 1390, 12:18 عصر
سلام. موقع لاگین به صفحه فقط با اولین کاربری که تعریف کردم می تونم وارد سایت بشم.بعد که یه کاربر جدید تعریف کنم و بخوام لاگین کنم وقتی روی دکمه لاگین کلیک کردم کاربر رو که شناسایی کرد دیگه به صفحات مورد نظر هدایت نمیشه و همونجا بقولی هنگ می کنه(انگار افتاده تو loop) ! نمی دونم مشکل از کجاست ممنون میشم راهنمایی کنید.
private string GetMemberRoleByID(string UID)
{
using (SqlConnection con = new SqlConnection(func.constr))
{
SqlCommand cmd = new SqlCommand("select Role from tblUsers where U_UserName=@id", con);
cmd.Parameters.AddWithValue("@id", UID);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
string RetVal = "NA";
if (dr.Read())
{
RetVal = dr["Role"].ToString();
}
con.Close();
return RetVal;
}
}
private bool GetMemberAuthenticate(string UID, string PWD)
{
using (SqlConnection con = new SqlConnection(func.constr))
{
bool RetVal = false;
SqlCommand cmd = new SqlCommand("select count(*) from tblUsers where (U_UserName=@uid And U_Pass=@Pwd)", con);
cmd.Parameters.AddWithValue("@uid", UID);
cmd.Parameters.AddWithValue("@Pwd", PWD);
con.Open();
RetVal = Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
return RetVal;
}
}
protected void LoginButton_Click(object sender, EventArgs e)
{
if (GetMemberAuthenticate(txtUserName.Text.Trim(), txtPassword.Text))
{
string role = GetMemberRoleByID(txtUserName.Text.Trim());
FormsAuthenticationTicket AuthTicket = new FormsAuthenticationTicket(1, txtUserName.Text.Trim(), DateTime.Now, DateTime.Now.AddHours(1), false, role);
string encryptedTicket = FormsAuthentication.Encrypt(AuthTicket);
HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(AuthCookie);
Response.Redirect(FormsAuthentication.GetRedirectU rl(txtUserName.Text.Trim(), RememberMe.Checked));
}
else
{
FailureText.Text = "نام کاربری یا رمز عبور صحیح نیست.";
}
}
این کد هارم توی فایل Global.asax نوشتم.
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName.ToString()];
if (authCookie != null)
{
FormsAuthenticationTicket MyTicket;
MyTicket = FormsAuthentication.Decrypt(authCookie.Value);
FormsIdentity MyIdentity = new FormsIdentity(MyTicket);
GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyTicket.UserData.Split(new char[] { ',' }));
Context.User = MyPrincipal;
}
}
این کدهارم توی MasterPage نوشتم :
if (!IsPostBack)
{
string uName = HttpContext.Current.User.Identity.Name;
using (SqlConnection con = new SqlConnection(func.constr))
{
SqlCommand cmd = new SqlCommand("select * from tblUsers where U_UserName=@userName", con);
cmd.Parameters.AddWithValue("@userName", uName);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string drRole = dr["Role"].ToString();
if (Page.User.Identity.IsAuthenticated)
{
if (Page.User.IsInRole(dr["Role"].ToString()))
{
if (drRole == "user")
{
// FormsAuthentication.RedirectFromLoginPage(uName, true);
Response.Redirect("~/Default.aspx");
}
if (drRole=="admin")
{
Response.Redirect("~/Admin/manDefault.aspx");
}
}
else if (!Page.User.IsInRole(dr["Role"].ToString()))
{
Response.Redirect("~/MsgPage.aspx");
}
else
{
// Normal User
}
}
else
{
FormsAuthentication.RedirectToLoginPage();
}
}
con.Close();
}
}
private string GetMemberRoleByID(string UID)
{
using (SqlConnection con = new SqlConnection(func.constr))
{
SqlCommand cmd = new SqlCommand("select Role from tblUsers where U_UserName=@id", con);
cmd.Parameters.AddWithValue("@id", UID);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
string RetVal = "NA";
if (dr.Read())
{
RetVal = dr["Role"].ToString();
}
con.Close();
return RetVal;
}
}
private bool GetMemberAuthenticate(string UID, string PWD)
{
using (SqlConnection con = new SqlConnection(func.constr))
{
bool RetVal = false;
SqlCommand cmd = new SqlCommand("select count(*) from tblUsers where (U_UserName=@uid And U_Pass=@Pwd)", con);
cmd.Parameters.AddWithValue("@uid", UID);
cmd.Parameters.AddWithValue("@Pwd", PWD);
con.Open();
RetVal = Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
return RetVal;
}
}
protected void LoginButton_Click(object sender, EventArgs e)
{
if (GetMemberAuthenticate(txtUserName.Text.Trim(), txtPassword.Text))
{
string role = GetMemberRoleByID(txtUserName.Text.Trim());
FormsAuthenticationTicket AuthTicket = new FormsAuthenticationTicket(1, txtUserName.Text.Trim(), DateTime.Now, DateTime.Now.AddHours(1), false, role);
string encryptedTicket = FormsAuthentication.Encrypt(AuthTicket);
HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(AuthCookie);
Response.Redirect(FormsAuthentication.GetRedirectU rl(txtUserName.Text.Trim(), RememberMe.Checked));
}
else
{
FailureText.Text = "نام کاربری یا رمز عبور صحیح نیست.";
}
}
این کد هارم توی فایل Global.asax نوشتم.
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName.ToString()];
if (authCookie != null)
{
FormsAuthenticationTicket MyTicket;
MyTicket = FormsAuthentication.Decrypt(authCookie.Value);
FormsIdentity MyIdentity = new FormsIdentity(MyTicket);
GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyTicket.UserData.Split(new char[] { ',' }));
Context.User = MyPrincipal;
}
}
این کدهارم توی MasterPage نوشتم :
if (!IsPostBack)
{
string uName = HttpContext.Current.User.Identity.Name;
using (SqlConnection con = new SqlConnection(func.constr))
{
SqlCommand cmd = new SqlCommand("select * from tblUsers where U_UserName=@userName", con);
cmd.Parameters.AddWithValue("@userName", uName);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string drRole = dr["Role"].ToString();
if (Page.User.Identity.IsAuthenticated)
{
if (Page.User.IsInRole(dr["Role"].ToString()))
{
if (drRole == "user")
{
// FormsAuthentication.RedirectFromLoginPage(uName, true);
Response.Redirect("~/Default.aspx");
}
if (drRole=="admin")
{
Response.Redirect("~/Admin/manDefault.aspx");
}
}
else if (!Page.User.IsInRole(dr["Role"].ToString()))
{
Response.Redirect("~/MsgPage.aspx");
}
else
{
// Normal User
}
}
else
{
FormsAuthentication.RedirectToLoginPage();
}
}
con.Close();
}
}