2011-01-27 46 views
1

我有一個asp.net應用程序,當用戶登錄....時,我使用FormAuthentication票證....每個頁面上我想檢查FormAuthentication票證是否已過期。FormAuthentication票據到期檢查

其實我有兩個場景

  1. 我要檢查,如果用戶進行身份驗證或試圖直接無需登錄即可訪問該頁面(在這種情況下,我想在「Default.aspx的」重定向

  2. 如果用戶已經簽名認證,但超時發生(在這種情況下,我想重定向到「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"); 
後面我的代碼

回答

0

沒有您的登錄/授權碼,很難確定您是如何設置的。

你應該做的,就是設置會話/ Cookie超時是會議的第一件事到期時間+ 1分(如21分鐘)

然後,你可以寫一個HttpModule檢查超時和重定向

public class ExpireModule : IHttpModule { 

    public virtual void Init(HttpApplication app) { 
     app.PostAuthenticateRequest += new EventHandler(app_PostAuthenticateRequest); 
    } 

    private void app_PostAuthenticateRequest(object sender, EventArgs e) { 
     //check ticket 
     //if old, kill login, redirect to session timeout page 
    } 
} 

還是做同樣的事情在一個共享的基本頁面(如果有的話)

通過使會話超時21分鐘,你可以使用所有標準的授權碼

+0

我已經編輯我的問題和粘貼完整代碼formauthentication登錄按鈕。夥計PLZ迴應我真的很感激。 – user239684 2011-01-28 03:16:47

0

要在用戶未被授權時設置要打開的默認頁面,請設置loginUrl

另外不要忘記檢查slidingExpiration未設置爲false

<forms 
    name=".ASPXFORMSAUTH" 
    loginUrl="Default.aspx" 
    defaultUrl="Default.aspx" 
    slidingExpiration="true" 
    timeout="30" /> 

MSDN


要檢查確實超時落下帷幕,使用的Global.asax事件Application_BeginRequest

public class Global : HttpApplication 
{ 
    protected virtual void Application_BeginRequest(object sender, EventArgs e) 
    { 
     if (!his.User.Identity.IsAuthenticated) 
      this.Response.Redirect("Timeout.aspx"); 
    }  
}