在C#中创建会话#

问题描述:

您好我正在创建一个登录表单从头开始在C#使用3层。我设法构建了一个工作表单来检查用户数据是否正确。如果他填写了错误的数据,他会收到一条消息。但是现在我需要创建一个会话来存储这个ID。在C#中创建会话#

我搜查了网页,他们说你必须添加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; 
     } 
    } 
} 
+0

请详细说明“他不承认任何事情。”此外,会话状态不会进入DAL或BLL。它完全属于Web应用程序。 – David 2011-12-15 17:13:18

+0

是否与用户用于登录的用户ID相同?如果是,那么你可以直接使用HttpContext.Current.User.Identity.Name.ToString()来访问它,只要你想要id数据,而不需要在会话中存储它 – 2011-12-15 17:23:55

.NET会话状态是在表示层中处理的,尽管它可以在web worker进程中运行的任何业务逻辑中访问(请注意,还存在进程外会话状态,但是也可以从表示层进行管理)。与表示层之外的会话进行交互是非常好的做法。

System.Web.HttpContext.Current.Session 

里面大多数网络实体(页面,控制,查看),也就是对Session引用:

在业务层,会话可以进行访问。

会话是一个基于密钥的集合;你用一个关键字赋值,然后用一个关键字检索相同的值。

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

访问会话只将在可用的Web应用程序,因此您需要设置并从会话中获取值,并将这些值从网络传递到其他图层。

您也可以使用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");