开发者

Login-logout in asp.net

I have a login page which works fine. I would like your help for how can i do the Logout. I send you the CustomerLogin.cs class whic I created. The login1 which i have is a call to my web service. Can anyone tell me what to do?

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 Init开发者_Python百科ializeComponent()
    {
        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"]);
        }


    }

} 


try to remove Session["userId"] by using Session.Remove("userId") and redirect to log in page, i'm not sure on login method what you are doing. if you change login status on that method you need to reset that again on the log out method.


Session.Abandon() will terminate the session.


The way I do it is this.

First, I put all Session variables in a static class. This way, I don't have disjoint strings all over the place (the name(s) of the session variables.

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";
    }
}

Then I use a static helper method to "clean things up".

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");
        }

    }
}

Then on the code behind of any asp.net page (.cs file), I can call this routine.

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);
        }
}

Whenever I add a value to SessionVariableKeys, I know I need to go to SessionHelpers to add a line to two to the clean up routine.

This prevents "oops, I forgot about YadaYadaYada session variable.

It prevents syntax errors with this style:

string myUserID = Session["userId"];

and later

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

(Aka, a silly syntax error that can mess up things). (Note the letter I vs. the number 1.)

I just prefer nice little tidy "helper" methods....over having the code strung out all over the place. And the "passing the context into a helper method" is the little trick to be able to do that.

Good luck with your endeavor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜