PDA

View Full Version : ارور در Application_AuthenticateRequest



mamad_za
شنبه 13 دی 1393, 17:22 عصر
دوستان من برنامه رو run می کنم ارور میده میگه که کوکی خالیه و در خط Decrypt این اشکال به وجود میاد، متن ارور :





Line 40: FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);




[/CSHARP]
HttpCookie cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
FormsIdentity identity = new FormsIdentity(ticket);
string[] roles = ticket.UserData.Split('|');
System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(identit y, roles);
Context.User = principal;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Select * from tbl_User WHERE UserName=@User",con))
{

using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@User", txtUserName.Text);
con.Open();
sda.Fill(dt);
con.Close();
string HashedPass = dt.Rows[0]["Pass"].ToString();
if (dt.Rows.Count > 0 && Code.ValidatePassword(txtPass.Text, HashedPass))
{
lblResult.Text = "khosh amadid";
if (FormsAuthentication.CookiesSupported)
{

var ticket = new FormsAuthenticationTicket(2, txtUserName.Text.Trim().ToLower(), DateTime.Now, DateTime.Now.AddMonths(2),true,"admin");
string encryptTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket)
{
Expires = chkRemember.Checked ? ticket.Expiration : DateTime.Now.AddHours(2)
};
Response.SetCookie(cookie);
}
else
{
throw new Exception("Cookieless Forms Authentication is not supported for this application.");
}

}