簡體   English   中英

在C#中創建會話

[英]Create session in C#

嗨,我正在使用3層從頭開始創建一個登錄表單。 我已經設法構建一個工作表單來檢查用戶數據是否正確。 如果他填寫了錯誤的數據,他會得到一條消息。 但現在我需要創建一個存儲id的會話。

我搜索了網頁,他們說你必須添加Session["sessionName"]= data ,但是如果我輸入Session["userId"]=s.studentNummer他什么都不識別。 將會話放在DAL或DLL中會更好嗎? 我想在DAL(函數checkLogin)中編寫它。 有人能幫幫我嗎?

這是我的代碼:

DALstudent.cs

public class DALstudent
{
    dc_databankDataContext dc = new dc_databankDataContext();

    public void insertStudent(Student s)
    {
        dc.Students.InsertOnSubmit(s);
        dc.SubmitChanges();
    }

    public bool checkLogin(string ID, string passw)
    {
        bool canlogin = false;
        var result = (from s in dc.Students
                      where s.studentNummer == ID && s.studentPasswoord == passw
                      select s).Count();
        if (result == 1)
        {
            canlogin = true;
        }
        else 
        {
            canlogin = false;
        }
        return canlogin;
    }
}

BLLstudent.cs

public class BLLstudent
{
    DALstudent DALstudent = new DALstudent();

    public void insertStudent(Student s)
    {
        DALstudent.insertStudent(s);
    }

    public string getMD5Hash(string passwd)
    {
        MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
        byte[] bs = Encoding.UTF8.GetBytes(passwd);
        bs = x.ComputeHash(bs);
        StringBuilder str = new StringBuilder();
        foreach (byte b in bs)
        {
            str.Append(b.ToString("x2").ToLower());
        }
        string password = str.ToString();
        return password;
    }

    public bool checkLogin(string ID, string passw)
    {
        bool canlogin = DALstudent.checkLogin(ID, passw);
        if (canlogin == true)
        {
            return true;
        }
        else 
        {
            throw new Exception("Uw gegevens kloppen niet");
        }
    }
}

login.aspx.cs

public partial class web_login : System.Web.UI.Page
{
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            BLLstudent BLLstudent = new BLLstudent();
            var loginNr = txtLoginNr.Text;
            var pass = BLLstudent.getMD5Hash(txtWachtwoord.Text);
            var passw = pass;
            BLLstudent.checkLogin(loginNr, passw);
            Response.Redirect("student/s_procedure_goedkeuring.aspx");
        }
        catch (Exception Ex) 
        {
            lblFeedback.Text = Ex.Message;
        }
    }
}

.NET會話狀態在表示層中處理,盡管它可以在Web工作進程中運行的任何業務邏輯中訪問(請注意,還存在進程外會話狀態,但也是從表示層管理的)。 與表示層之外的會話進行交互很少是一種好的做法。

在業務層中,可以使用以下方式訪問會話:

System.Web.HttpContext.Current.Session

在大多數Web實體(頁面,控件,視圖)中,它只是由Session引用。

Session是一個基於密鑰的集合; 您使用鍵放入值,並使用鍵檢索相同的值。

protected override void OnLoad( EventArgs e )
{
    Session["foo"] = "bar";
    string valueFromSession = Session["foo"].ToString();
}

您還可以在會話中使用Cookie:

if (SessionHash != null && (!HttpContext.Current.Request.Cookies.AllKeys.Contains("hash")) {
  var cookie = new HttpCookie("hash", Convert.ToBase64String(SessionHash)) {
    HttpOnly = true
  };

  HttpContext.Current.Response.Cookies.Set(cookie);
}

// remove cookie on log out.
HttpContext.Current.Request.Cookies.Remove("hash");

只有在Web應用程序中才能訪問會話,因此您需要設置並從會話中獲取值,並將這些值從Web傳遞到其他層。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM