我有一個asp.net應用程序,當用戶登錄....時,我使用FormAuthentication票證....每個頁面上我想檢查FormAuthentication票證是否已過期。FormAuthentication票據到期檢查
其實我有兩個場景
我要檢查,如果用戶進行身份驗證或試圖直接無需登錄即可訪問該頁面(在這種情況下,我想在「Default.aspx的」重定向
如果用戶已經簽名認證,但超時發生(在這種情況下,我想重定向到「sexpired.aspx」頁面,用戶將被通知「你的會話已過期,請重新登錄」,鏈接爲「 Default.aspx「,它會重定向回到url,請告知並提出相應的解決方案。
目前,我的每一頁上這樣做的,我認爲,當cookie過期它使User.Identity.IsAuthenticated = false
也造成超時,當用戶試圖加載它重定向回「的Default.aspx」
好這裏的頁面是我背後的登錄表單代碼更新問題:
protected void LoginButton_Click(object sender, EventArgs e)
{
if (AuthenticateUser("SPOINT", txtUsername.Text, txtPassword.Text))
{
//Fetch the role
Database db = DatabaseFactory.CreateDatabase();
//Create Command object
DbCommand cmd = db.GetStoredProcCommand("Users");
db.AddInParameter(cmd, "@userid", System.Data.DbType.String, 20);
db.SetParameterValue(cmd, "@userid", txtUsername.Text);
db.AddInParameter(cmd, "@fname", System.Data.DbType.String, 80);
db.SetParameterValue(cmd, "@fname", null);
db.AddInParameter(cmd, "@lname", System.Data.DbType.String, 80);
db.SetParameterValue(cmd, "@lname", null);
db.AddInParameter(cmd, "@phone", System.Data.DbType.String, 50);
db.SetParameterValue(cmd, "@phone", null);
db.AddInParameter(cmd, "@mobile", System.Data.DbType.String, 50);
db.SetParameterValue(cmd, "@mobile", null);
db.AddInParameter(cmd, "@email", System.Data.DbType.String, 100);
db.SetParameterValue(cmd, "@email", null);
db.AddInParameter(cmd, "@uroleids", System.Data.DbType.String, 50);
db.SetParameterValue(cmd, "@uroleids", null);
db.AddInParameter(cmd, "@uroles", System.Data.DbType.String, 500);
db.SetParameterValue(cmd, "@uroles", null);
db.AddInParameter(cmd, "@umenu", System.Data.DbType.Int16);
db.SetParameterValue(cmd, "@umenu", null);
db.AddInParameter(cmd, "@ustatus", System.Data.DbType.String, 1);
db.SetParameterValue(cmd, "@ustatus", null);
db.AddInParameter(cmd, "@reqType", System.Data.DbType.String, 1);
db.SetParameterValue(cmd, "@reqType", "R");
db.AddOutParameter(cmd, "@retval", DbType.Int16, 2);
IDataReader reader = db.ExecuteReader(cmd);
System.Collections.ArrayList roleList = new System.Collections.ArrayList();
if (reader.Read())
{
roleList.Add(reader[0]);
string myRoles = (string)roleList[0];
//Read user name
string uname = (string)reader[1];
//Read User menu ID
int menuID = Convert.ToInt16(reader[2]);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now,
DateTime.Now.AddMinutes(30), true, myRoles, FormsAuthentication.FormsCookiePath);
//Read user full name in session variable which will be shared across the whole application
Session["uid"] = txtUsername.Text;
Session["ufullname"] = uname; //myname; //uname;
Session["branch"] = 1;
//For security reasons we may hash the cookies
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
// add the cookie to user browser
Response.Cookies.Add(cookie);
//Constructing Menu according to User Role
string x = buildmenu(menuID);
Globals.menuString = null;
Globals.menuString = x;
string returnURL = "~/Main.aspx";
//Close reader object to avoid Connection Pooling troubles
reader.Close();
if (Request.QueryString["rUrl"] != null)
Response.Redirect(Request.QueryString["rUrl"]);
else
Response.Redirect(returnURL);
}
else
{
//Validation Error here...
lblError.Text = "Incorrect UserID/Password entered...";
return;
}
}
else
{
lblError.Text = "Incorrect UserID/Password entered...";
return;
}
}
這裏是我檢查formauthentication票
if (!HttpContext.Current.User.Identity.IsAuthenticated || !HttpContext.Current.User.IsInRole("Maker"))
Response.Redirect("~/Default.aspx");
後面我的代碼
我已經編輯我的問題和粘貼完整代碼formauthentication登錄按鈕。夥計PLZ迴應我真的很感激。 – user239684 2011-01-28 03:16:47