简体   繁体   English

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

[英]How do I save Log In session in ASP.NET MVC

Hi this is my Login Controller for the Login page and I have done everything to check and login however I have not made the Login Session so my website does not know whoever login using the IDs and Password from the database.嗨,这是我登录页面的登录 Controller,我已经完成了所有检查和登录,但是我没有登录 Session,所以我的网站不知道谁使用数据库中的 ID 和密码登录。 So the website only recognizes one user even after the login has been made.因此,即使在登录后,该网站也只能识别一个用户。

I also need to know how to retrieve the login session if I am to make a selection using a button.如果我要使用按钮进行选择,我还需要知道如何检索登录 session。 For example;例如; "User Z selected WorkSchedule A *after logging in with User Z's username and password" “用户 Z 选择WorkSchedule A *使用用户 Z 的用户名和密码登录后”

A login wouldn't be complete if there is no saved session for the website, I have troubles making the save session and would appreciate if someone could guide me towards making it.如果网站没有保存 session,则登录将不完整,我在保存 session 时遇到了麻烦,如果有人能指导我制作它,我将不胜感激。

Controller code: Controller 代码:

 [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");
        }


    }

View Page:查看页面:

    <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>

First, from your description, it seems that your application is an Asp.net MVC application, instead of Asp.net Core MVC application.首先,根据您的描述,您的应用程序似乎是 Asp.net MVC 应用程序,而不是 Asp.net Core MVC 应用程序。 So, you could directly use a session to store the User information in the session:因此,您可以直接使用 session 将用户信息存储在 session 中:

Code as below, save value to session:代码如下,将值保存到 session:

Session("UserName") = UserName;

Then, when you read data from the session, you could use the following code:然后,当您从 session 读取数据时,您可以使用以下代码:

        if (Session["UserName"] != null)  
        {  
            return View();  
        } else  
        {  
            return RedirectToAction("Login");  
        } 

More detail information about session state management in asp.net, please check the following articles:关于 asp.net 中的 session state 管理的更多详细信息,请查看以下文章:

ASP.NET Session State Overview ASP.NET Session State 概述

Simple Login Application using Sessions in ASP.NET MVC 在 ASP.NET MVC 中使用会话的简单登录应用程序

Second, Asp.net provides a Identity system, we could use its build-in method to achieve Login function, Manager users, roles and more and store login user information with cookie.其次,Asp.net提供了一个Identity system,我们可以使用它的内置方法来实现Login function,Manager用户,角色等,并通过cookie存储登录用户信息。 You could try to use it with your MVC application.您可以尝试将它与您的 MVC 应用程序一起使用。 Please check the following articles:请检查以下文章:

Introduction to ASP.NET Identity ASP.NET身份介绍

Adding ASP.NET Identity to an Empty or Existing Web Forms Project 将 ASP.NET 身份添加到空的或现有的 Web Forms 项目

Adding ASP.NET MVC5 Identity Authentication to an existing project 将 ASP.NET MVC5 身份验证添加到现有项目

Besides, if your application is Asp.net Core MVC application, you have to enable the session middleware in Startup.cs.此外,如果您的应用程序是 Asp.net Core MVC 应用程序,则必须在 Startup.cs 中启用 session 中间件。 More detail information about using session and Identity in asp.net core, you could check the following articles:有关在 asp.net 内核中使用 session 和 Identity 的更多详细信息,您可以查看以下文章:

Session and state management in ASP.NET Core ASP.NET 内核中的 Session 和 state 管理

Introduction to Identity on ASP.NET Core ASP.NET内核上的身份介绍

How add ASP.NET Core identity to existing Core mvc project? 如何将 ASP.NET 核心标识添加到现有的核心 mvc 项目中?

  1. First of all your code is vulnerable for sql injects.首先,您的代码容易受到 sql 注入的攻击。
  2. MVC project has build in user authentication system, why not to use it? MVC 项目已经内置了用户认证系统,为什么不使用呢?
  3. If you don't want to use build in user authentication, you can implement cookie which will store current user session.如果您不想使用内置用户身份验证,则可以实现 cookie,该 cookie 将存储当前用户 session。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否需要一个数据库来保存有限的数据集,并且仅用于ASP.NET MVC应用程序中的当前会话? - Do I need a database to save a finite set of data, and only for the current session in an ASP.NET MVC app? 如何将POST和GET数据保存到会话中,并使用ASP.NET C#将其提取? - How do i save POST and GET data into a session and pull it up with ASP.NET C#? 如何在ASP.NET MVC中使用Session_Start? - How do I use Session_Start in ASP.NET MVC? 将注释保存到asp.net mvc应用程序中的数据库时,如何保持注释的格式? - How do I maintain formatting of comments when I save them to the database in an asp.net mvc application? 如何在ASP.NET MVC中关闭浏览器或选项卡时将用户注销? - How do I log a user out when they close their browser or tab in ASP.NET MVC? 如何将捕获到的ASP.NET MVC3异常记录到文件? - How do I log caught ASP.NET MVC3 exception to file? ASP.NET MVC-如何从asp.net网站删除Cookie中的日志 - ASP.NET MVC - How do Remove log in Cookies from a asp.net site 如何将字符串形式的选择列表传递给控制器​​,以在asp.net MVC 5中另存为int值 - How do I pass selectlist that is string to controller to save as a int value in asp.net mvc 5 我如何使用实体框架ASP.NET MVC5保存相关数据? - How do i save related data using entity framework asp.net mvc5? 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM