【问题标题】:Create session in C#在 C# 中创建会话
【发布时间】:2012-01-21 09:00:56
【问题描述】:

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

【问题讨论】:

  • 请详细说明“他什么都不认识”。此外,会话状态不会进入 DAL 或 BLL。它完全属于 Web 应用程序。
  • 你的用户ID和用户登录的ID一样吗?如果是,那么您可以在需要 id 数据时使用 HttpContext.Current.User.Identity.Name.ToString() 直接访问它,而无需将其存储在会话中
  • MD5 不是安全哈希。使用 SHA1 或 SHA2,别忘了加盐。

标签: c# asp.net .net session


【解决方案1】:

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

在业务层,可以通过以下方式访问会话:

System.Web.HttpContext.Current.Session

在大多数网络实体(页面、控件、视图)中,它只是由Session 引用。

Session 是一个基于键的集合;你用一个键输入一个值,然后用一个键检索相同的值。

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

【讨论】:

  • 谢谢!!!我明白了(它有效)。我虽然必须把它放在查询所在的 DAL 中,就像我在 php 中所做的那样。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多