2011-08-27 6 views
0

잘 작동하는 로그인 페이지가 있습니다. 로그 아웃을 어떻게 할 수 있는지에 대한 도움을 받고 싶습니다. 내가 만든 CustomerLogin.cs 클래스를 보냅니다. 내가 가진 login1 내 웹 서비스에 대한 호출입니다. 아무도 나에게 무엇을해야한다고 말할 수 있습니까?로그인 - 로그 아웃 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

내가하는 방식입니다.

먼저 모든 세션 변수를 정적 클래스에 넣습니다. 이 방법은, 나는 모든 세션 변수의 장소 (이름 (들)을 통해 분리 된 문자열이 없습니다.

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에 갈 필요가 알고 깨끗한 곳에 두 줄 추가 up 루틴.

이 방지

"죄송합니다, 나는 YadaYadaYada 세션 변수 잊어

그것은이 스타일 구문 오류를 방지 :.

string myUserID = Session["userId"]; 

이후

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

(일명, 바보 같은 구문 엉망으로 만들 수있는 오류). (문자 I 대 숫자 1에 유의하십시오.)

난 그냥 좋은 작은 깔끔한 "도우미"방법을 선호 .... 코드 곳곳에 중독되었습니다. 그리고 "도우미 방법으로 상황을 전달하는 것"은 그 일을 할 수있는 작은 트릭입니다.

귀하의 노력과 함께 행운을 빕니다.