PDA

View Full Version : سوال: چرا بعد از 10min تيكت expire ميشه !!!؟



merlin_vista
سه شنبه 04 تیر 1387, 19:06 عصر
سلام :
من براي اهراز هويت از تيكت و Role استفاده ميكنم . و اين كد من هستش براي ساخت تيكت :

// Create a new ticket used for authentication

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version

this.txt_username.Text, // Username associated with ticket

DateTime.Now, // Date/time issued

DateTime.Now.AddHours(3), // Date/time to expire

true, // "true" for a persistent user cookie

reader.GetString(0), // User-data, in this case the roles

FormsAuthentication.FormsCookiePath);// Path cookie valid for


// Encrypt the cookie using the machine key for secure transport

string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie

hash); // Hashed ticket


// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

همان طور كه ميبينيد . مدت اكسپاير 3 ساعت هست ولي بعد از 10 دقيقه اكسپاير ميشه . ممكنه بگيد از چيه ؟ :متعجب:

Behrouz_Rad
سه شنبه 04 تیر 1387, 19:23 عصر
خوب بقیش کو؟
کد کامل رو بگذار...

پ.ن: من ارادت خاصی نسبت به تمامی کسانی که از Forms Authentication استفاده می کنند دارم :D

merlin_vista
چهارشنبه 05 تیر 1387, 01:11 صبح
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated == true)
{
Response.Redirect(FormsAuthentication.DefaultUrl.T oString(), false);
}


}
protected void btn_login_Click(object sender, EventArgs e)
{
FormsAuthentication.Initialize();
SqlCommand cmd = new SqlCommand("login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", this.txt_username.Text);
cmd.Parameters.AddWithValue("@password",obj_cls.encryptString(this.txt_password.Text));
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

if (reader.Read())
{
// Create a new ticket used for authentication

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version

this.txt_username.Text, // Username associated with ticket

DateTime.Now, // Date/time issued

DateTime.Now.AddHours(3), // Date/time to expire

true, // "true" for a persistent user cookie

reader.GetString(0), // User-data, in this case the roles

FormsAuthentication.FormsCookiePath);// Path cookie valid for


// Encrypt the cookie using the machine key for secure transport

string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie

hash); // Hashed ticket


// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
// Add the cookie to the list for outgoing response


//IF USER STATUS

if (reader.GetBoolean(1) != true)
{
this.lbl_errore.Visible = true;
this.txt_password.Text = null;
return;
}
if (reader.GetBoolean(2) == true)
{
this.lbl_errore.Visible = true;
this.txt_password.Text = null;
return;
}
//if (reader.GetBoolean(3) != true)
//{
// SqlCommand cmd_updateOnline = new SqlCommand("updateOnline", con);
// cmd_updateOnline.CommandType = CommandType.StoredProcedure;
// cmd_updateOnline.Parameters.AddWithValue("@id", reader.GetInt32(4));
// reader.Close();
// cmd_updateOnline.ExecuteNonQuery();
//}
string redirectURL = "~/user/Default.aspx";
if (Request.QueryString["ReturnUrl"]!=null)
{
redirectURL = Request.QueryString["ReturnUrl"].ToString();
}
Response.Cookies.Add(cookie);
Response.Redirect(redirectURL,false);

}
else
{
this.lbl_errore.Visible = true;
this.txt_password.Text = null;

}


}

اين هم كل كد صفحه لوگين اگه WebConfig و Global هم احتياج هست بگيد تا بگزارم

smrb59
چهارشنبه 05 تیر 1387, 01:34 صبح
من ارادت خاصی نسبت به تمامی کسانی که از Forms Authentication استفاده می کنند دارم :D

آقای راد من هم از Forms Authentication استفاده میکنم ولی وقتی که رو دکمه خروج میزنم از اون برنامه خارج نمیشه و هنوز منو میشناسه.


FormsAuthentication.SignOut();

Behrouz_Rad
چهارشنبه 05 تیر 1387, 10:15 صبح
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated == true)
{
Response.Redirect(FormsAuthentication.DefaultUrl.T oString(), false);
}


}
protected void btn_login_Click(object sender, EventArgs e)
{
FormsAuthentication.Initialize();
SqlCommand cmd = new SqlCommand("login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", this.txt_username.Text);
cmd.Parameters.AddWithValue("@password",obj_cls.encryptString(this.txt_password.Text));
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

if (reader.Read())
{
// Create a new ticket used for authentication

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version

this.txt_username.Text, // Username associated with ticket

DateTime.Now, // Date/time issued

DateTime.Now.AddHours(3), // Date/time to expire

true, // "true" for a persistent user cookie

reader.GetString(0), // User-data, in this case the roles

FormsAuthentication.FormsCookiePath);// Path cookie valid for


// Encrypt the cookie using the machine key for secure transport

string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie

hash); // Hashed ticket


// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
// Add the cookie to the list for outgoing response


//IF USER STATUS

if (reader.GetBoolean(1) != true)
{
this.lbl_errore.Visible = true;
this.txt_password.Text = null;
return;
}
if (reader.GetBoolean(2) == true)
{
this.lbl_errore.Visible = true;
this.txt_password.Text = null;
return;
}
//if (reader.GetBoolean(3) != true)
//{
// SqlCommand cmd_updateOnline = new SqlCommand("updateOnline", con);
// cmd_updateOnline.CommandType = CommandType.StoredProcedure;
// cmd_updateOnline.Parameters.AddWithValue("@id", reader.GetInt32(4));
// reader.Close();
// cmd_updateOnline.ExecuteNonQuery();
//}
string redirectURL = "~/user/Default.aspx";
if (Request.QueryString["ReturnUrl"]!=null)
{
redirectURL = Request.QueryString["ReturnUrl"].ToString();
}
Response.Cookies.Add(cookie);
Response.Redirect(redirectURL,false);

}
else
{
this.lbl_errore.Visible = true;
this.txt_password.Text = null;

}


}

اين هم كل كد صفحه لوگين اگه WebConfig و Global هم احتياج هست بگيد تا بگزارم

اون شیوه ی کد نویسی مال VS 2003 هست که به نظرم قبلا در یک chm دیدمش که البته اشتباه است!
به جای:


Response.Redirect(redirectURL,false);

بنویس:


Response.Redirect(FormsAuthentication.GetRedirectU rl(txtUsername.Text, True), True)



آقای راد من هم از Forms Authentication استفاده میکنم ولی وقتی که رو دکمه خروج میزنم از اون برنامه خارج نمیشه و هنوز منو میشناسه.

بعد از اینکه logout کردی باید به خود صفحه مجددا Redirect کنی.
وقتی logout می کنی، کوکی برای Expire شدن تیک می خوره اما همچنان وجود داره. وقتی که به خود صفحه Redirect می کنی، مرورگر کوکی Expire شده رو تشخیص میده و اون رو حذف می کنه.

موفق باشید.

merlin_vista
چهارشنبه 05 تیر 1387, 12:37 عصر
اون شیوه ی کد نویسی مال VS 2003 هست که به نظرم قبلا در یک chm دیدمش که البته اشتباه است!

ميشه بگيد روش جديد چيه ، يا يك فايل و يا سايت آموزشي دربارش معرفي كنيد .

چرا ميگيد اشتباه است ، مشكلش چيه كه ؟؟؟؟؟!!!!

ممنون از راهنمايي خوب شما !

yasak55
چهارشنبه 05 تیر 1387, 12:48 عصر
سلام،


همان طور كه ميبينيد . مدت اكسپاير 3 ساعت هست ولي بعد از 10 دقيقه اكسپاير ميشه . ممكنه بگيد از چيه ؟ :متعجب:


آیا همیشه دقیقا سر 10 دقیقه Expire می شه یا اینکه زمان های expire شدن random و نزدیک به دقیقه هست؟

merlin_vista
چهارشنبه 05 تیر 1387, 13:29 عصر
نه بين 10 تا 15 دقيقه هست !

Behrouz_Rad
چهارشنبه 05 تیر 1387, 13:50 عصر
مشکلت حل نشد؟ اگه نشد روی لوکال این مشکل هست یا روی سرور؟

yasak55
چهارشنبه 05 تیر 1387, 14:05 عصر
سلام،


نه بين 10 تا 15 دقيقه هست !


در صورت امکان سایت رو توی یک Application Pool جداگانه قرار بده و ببین زمان رو بهبود می بخشه؟ برای ایجاد App Pool از داخل IIS باید مراحل رو پیگیری کنی.

merlin_vista
چهارشنبه 05 تیر 1387, 16:45 عصر
مشکلت حل نشد؟ اگه نشد روی لوکال این مشکل هست یا روی سرور؟

ممنون آقاي راد ، مشكل حل شد . كدي را كه داديد جايگزين كردم مشكل رفع شد .

با سپاس فراوان از شما .!...