PDA

View Full Version : مشکل در لاگین کاربر با تشخیص هویت پیشرفته



kurdistan132
سه شنبه 01 شهریور 1390, 11: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();

}

}

hjran abdpor
چهارشنبه 02 شهریور 1390, 00:23 صبح
تو وب کانفیک چه کار کردی؟؟؟؟؟
این را به Else if تبدیل کن ببین حل میشه ؟؟؟؟؟
if (drRole=="admin")

{

Response.Redirect("~/Admin/manDefault.aspx");

}

kurdistan132
چهارشنبه 02 شهریور 1390, 09:47 صبح
hjran abdpor عزیز مشکل حل نشد.
تو وب کانفیگ هم اینو نوشتم :

<authentication mode="Forms">
<forms name="MyAppCookie" loginUrl="~/Login.aspx" defaultUrl="~/Default.aspx" slidingExpiration="true" timeout="30">
</forms>
</authentication>

kurdistan132
دوشنبه 07 شهریور 1390, 18:31 عصر
کسی راه حلی پیدا نکرد ؟

hjran abdpor
سه شنبه 08 شهریور 1390, 01:10 صبح
شما برنامه را ضمیمه کنید تا تست کنم.
افتخار میکنم که میبینم کردها هم تو سایت فعالیت دارند..

kurdistan132
شنبه 12 شهریور 1390, 09:42 صبح
فعالان فروم ! کسی نظری نداره ؟

hjran abdpor
شنبه 12 شهریور 1390, 12:42 عصر
با سلام.
دوست عزیز شما براتون را ضمیمه کنید تا Debug بشه و زودتر به جواب برسی.