如何在ASP.NET MVC中保存登录会话

3

嗨,这是我的登录控制器,用于登录页面。我已经完成了所有的检查和登录工作,但是我没有创建登录会话,因此我的网站不知道使用数据库中的ID和密码登录的用户是谁。所以即使进行了登录,网站也只能识别一个用户。

如果我要使用按钮进行选择,我还需要知道如何检索登录会话。例如:“用户Z在使用用户名和密码登录后选择了 WorkSchedule A”

如果没有为网站保存会话,登录将不完整。我在保存会话方面遇到了问题,希望有人能指导我如何解决。

控制器代码:

 [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    void connectionString()
    {
        con.ConnectionString = " ";

    }
    [HttpPost]
    public ActionResult SaveData(Account acc)
    {
        connectionString();
        con.Open();
        com.Connection = con;
        com.CommandText = "insert into Staff (StaffNRIC,StaffEmail,StaffContact,StaffName,StaffAddress,BranchID,StaffRole,StaffPositionID,StaffAccountStatus)" +
            "values ('" + acc.StaffNRIC + "','" + acc.StaffEmail + "','" + acc.StaffContact + "','" + acc.StaffName + "','" + acc.StaffAddress + "','" + acc.BranchID + "',' NULL ','" + acc.StaffPositionID + "', 'Pending' )";
        dr = com.ExecuteReader();
        if (dr.Read())
        {
            con.Close();
            return View("Register");
        }
        else
        {
            con.Close();
            return View("Login");
        }


    }
    [HttpPost]
    public ActionResult Verify(Account acc)
    {
        connectionString();
        con.Open();
        com.Connection = con;
        com.CommandText = "select * from Staff where StaffNRIC='" + acc.StaffNRIC + "' and StaffContact='" + acc.StaffContact + "' and StaffAccountStatus = 'Approved'";
        dr = com.ExecuteReader();
        if (dr.Read())
        {
            con.Close();

            return View("Home");
        }
        else
        {
            con.Close();
            return View("Login");
        }


    }

查看页面:

    <form action="Verify" method="post">
        <div class=" w3l-form-group">
            <label>NRIC:</label>
            <div class="group">
                <i class="fas fa-user"></i>
                <input type="text" name="StaffNRIC" class="form-control" placeholder="StaffNRIC" required="required">
            </div>
        </div>
        <div class=" w3l-form-group">
            <label>Password:</label>
            <div class="group">
                <i class="fas fa-unlock"></i>
                <input type="password" name="StaffContact" class="form-control" placeholder="StaffContact" required="required">
            </div>
        </div>

        <button type="submit">Login</button>
    </form>
</div>
2个回答

2
首先,根据您的描述,似乎您的应用程序是一个 Asp.net MVC 应用程序,而不是 Asp.net Core MVC 应用程序。因此,您可以直接使用会话来存储用户信息:
以下是代码示例,将值保存到会话中:
Session("UserName") = UserName;

然后,当您从会话中读取数据时,您可以使用以下代码:
        if (Session["UserName"] != null)  
        {  
            return View();  
        } else  
        {  
            return RedirectToAction("Login");  
        } 

关于ASP.NET中的会话状态管理的更详细信息,请查看以下文章: ASP.NET会话状态概述 在ASP.NET MVC中使用会话实现简单的登录应用程序 其次,Asp.net提供了一个身份验证系统,我们可以使用其内置方法实现登录功能,管理用户、角色等,并使用cookie存储登录用户信息。您可以尝试在MVC应用程序中使用它。请参阅以下文章: ASP.NET Identity简介 将ASP.NET Identity添加到空白或现有Web Forms项目中

将ASP.NET MVC5身份验证添加到现有项目中

此外,如果您的应用程序是Asp.net Core MVC应用程序,则必须在Startup.cs中启用会话中间件。有关在asp.net core中使用会话和身份验证的详细信息,请查看以下文章:

ASP.NET Core中的会话和状态管理

ASP.NET Core上的身份验证简介

如何将ASP.NET Core身份验证添加到现有的Core MVC项目中?


保存登录信息在会话中是否是一个好的方法? - Ali.Rashidi

0
  1. 首先,你的代码容易受到 SQL 注入攻击。
  2. MVC 项目内置了用户认证系统,为什么不使用它呢?
  3. 如果你不想使用内置的用户认证系统,可以实现一个 cookie 来存储当前用户会话。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接