2011-08-27 156 views
0

我有一個登錄頁面,工作正常。我希望得到您的幫助,我該如何註銷。我向您發送我創建的CustomerLogin.cs類。我擁有的login1是對我的web服務的調用。任何人都可以告訴我該怎麼辦?在asp.net登錄註銷

public partial class CustomerLogin : System.Web.UI.Page 
{ 
    protected login1.login CustomerLog; 

    public CustomerLogin() 
    { 
     Page.Init += new System.EventHandler(Page_Init); 
    } 

    private void Page_Load(object sender, System.EventArgs e) 
    { 
     if (Session["userId"] != null) 
     { 
      Server.Transfer("FirstPage.aspx"); 
     } 

    } 

    private void Page_Init(object sender, EventArgs e) 
    { 
     // 
     // CODEGEN: This call is required by the ASP.NET Web Form Designer. 
     // 
     InitializeComponent(); 
    } 

    #region Web Form Designer generated code 
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.submitCusLogin.Click += new System.EventHandler(this.submitCusLogin_Click); 
     this.Load += new System.EventHandler(this.Page_Load); 

    } 
    #endregion 

    private void submitCusLogin_Click(object sender, System.EventArgs e) 
    { 
     string customerEmail; 
     string customerPassword; 

     customerEmail = txtEmail.Text; 
     customerPassword = txtPassword.Text; 

     //Make a call to the Web Service Login 
     CustomerLog = new login1.login(); 
     //Access the Web method Customer_Details 
     string getId = CustomerLog.Customer_Details(customerEmail, customerPassword); 
     //Return a value.Check the value of the variable resultId and 
     //either grant the customer access or return an error message. 
     if (getId == "-1") 
     { 
      loginLabel.Text = "Invalid Login please re-enter your password and email!"; 


     } 
     else 
     { 
      loginLabel.Text = "Welcome"; 
      Session["userId"] = int.Parse(getId); 
      Server.Transfer((string)Session["return2Page"]); 
     } 


    } 

} 
+0

我不知道爲什麼人們不使用ASP.NET內置的登錄控件 – Maysam

+0

@Maysam,http://stackoverflow.com/a/6195965/368472 – Pratik

回答

0

嘗試使用Session.Remove("userId")刪除會話[「用戶id」]並重定向到登錄頁面,我不知道在登錄方法,你在做什麼。如果您更改該方法的登錄狀態,則需要在註銷方法中重新進行重置。

0

Session.Abandon()將終止會話。

0

我這樣做的方式是這樣的。

首先,我把所有的Session變量放在一個靜態類中。這樣一來,我沒有不相交的串遍會話變量的地方(姓名(或名稱)。

namespace MyCompany.Applications.MyApplication.Presentation.Web.Keys 
{ 
    internal static class SessionVariableKeys 
    { 
     internal static readonly string USER_ID = "UserId"; 
     internal static readonly string RETURN_TO_PAGE = "return2Page"; 
    } 
} 

然後我用一個靜態輔助方法爲「乾淨的東西了」。

namespace MyCompany.Applications.MyApplication.Presentation.Web.Helpers 
{ 

    internal static class SessionHelpers 
    { 

     internal static void LogoutUser(System.Web.HttpContext context) 
     { 
      context.Session[SessionVariableKeys.USER_ID] = null; 
      context.Session[SessionVariableKeys.RETURN_TO_PAGE] = null; 
      /* or */ 
      context.Session.Remove(SessionVariableKeys.USER_ID); 
      context.Session.Remove(SessionVariableKeys.RETURN_TO_PAGE); 
      /* or */ 
      context.Session.RemoveAll(); 
      /* and */ 
      /* icing on the cake */ 
      context.Session.Abandon(); 

      context.Response.Redirect("SomePage.aspx"); 
     } 

    } 
} 

然後在任何asp.net頁面(cs文件)背後的代碼,我可以調用這個程序。

namespace MyCompany.Applications.MyApplication.Presentation.Web 
{ 
    public partial class MyPage : System.Web.UI.Page 
    { 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!Page.IsPostBack) 
      { 
      } 
     } 

     private void LogoutUser() 
     { 
      Helpers.SessionHelpers.LogoutUser(this.Context); 
     } 
} 

每當我添加了一個值SessionVariableKeys,我知道我需要去SessionHelpers到添加一行到兩個乾淨打開程序。

這可以防止「哎呀,我忘了YadaYadaYada會話變量

它可以防止語法錯誤,這種風格:

string myUserID = Session["userId"]; 

後來

string myValue = string.Empty; 
Session["user1d"] = myValue; 

(阿卡,一個愚蠢的語法(注意字母I與數字1的對應關係)。

我只是喜歡漂亮的小小整潔的「幫手」方法....在代碼遍佈各處。 「將上下文傳遞給輔助方法」是能夠做到這一點的小竅門。

祝你好運與你的努力。