2011-08-27 159 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的对应关系)。

我只是喜欢漂亮的小小整洁的“帮手”方法....在代码遍布各处。 “将上下文传递给辅助方法”是能够做到这一点的小窍门。

祝你好运与你的努力。