Using ASP.NET MVC framework to build login site summary

After a few days of simple learning of ASP.NET MVC framework and building a simple login site, I have a more overall understanding of Microsoft's MVC framework, including its structure on VS, the relationship between MVC layers, etc. Here I make a simple personal learning record and summarize the small project of building a login site.

In my opinion, the MVC framework of. NET is a classic MVC framework. Because I have used the MVC framework to develop small projects in Linux system before, the nature of the MVC framework will not change. It is nothing more than migrating it from linux environment to Windows environment and from shell to VS2019.

First, briefly introduce the MVC framework. In the VS2019 environment, select the MVC Model in the ASP.NET Web application we created. After entering the Model, the MVC corresponds to the Model layer (instance Model layer), View layer (presentation interface layer) and Controller layer respectively. In the Model layer, we can create Model classes that meet our needs, In this Model class, we encapsulate the data we need; The most important is the Controller layer. In this layer, we create the corresponding Controller. The Controller class is our operation on the data in the encapsulated Model class, that is, the so-called business logic (execution method). After we write the business logic code, we can create the corresponding View layer by adding a View through the method name, You can create an empty View class, and then write the corresponding cshtml code according to your needs. You can also choose to let the compiler help us generate the corresponding cshtml code in the View class. We only need to make some changes. In this way, the connection between MVC layers in VS2019 is established.

Here's how to make a login site:

Combined with the above thinking, first of all, we clarify the need to be a login site, which is similar to the login of QQ or a platform. If the login is successful, you will enter the main interface, and if it fails, you will be prompted that the user's user name or password is wrong

Then we should encapsulate the corresponding user name and password information in the model class. This time, we choose to create a new project, select a class library, and create a C# class library file. In this file, we complete the encapsulation of the model layer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MineLogin.Comment.User
{
    public class LoginUser
    {
        public string Name { get; set; }
        public string PassWord { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public long? QQ { get; set; }
    }
}

Note that the Model layer encapsulated in the new project class library file is not created under the Model folder in the created ASP.NET Web application!

After encapsulating our underlying user information, we need to create a new controller under the Controllers file in the ASP.NET Web application, and introduce the namespace of the class library Model we established in the previous step, so as to complete our controller code writing:

using MineLogin.Comment.User;       //Notice the namespace introduced here!
using MineLogin.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;

namespace MineLogin.Controllers
{
    public class AccountController : Controller
    {
        // GET: Account

        [HttpGet]   //Return to the page and display the login page
        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]    //Submit login and execute business logic at the beginning of login
        public ActionResult Login(LoginUser user)   
        {
            if (ModelState.IsValid)     //Trigger entity verification. If true is returned, the login is successful through verification
            {
                using(LjcTestContest contest = new LjcTestContest())
                {
                    int count = contest.Set<t_user>().Count(c => c.username == user.Name && c.password == user.PassWord);
                    if(count > 0)
                    {
                        return base.Redirect("/Home/Index");
                    }
                    else
                    {
                        return Content("Wrong user name or password!");
                    }
                }
            }
            return base.Redirect("Home/Index");
        }
    }
}

Of course, because my controller code is linked to the SQL Server database, I have the using MineLogin.DB namespace and a Set object in the judgment logic. If it is simpler, the database can not be used. Just write the user name and password in if, so as to control the Model layer code, Next, you need to right-click the Login class name from the above code logic block, and then add the view. I simply modify the cshtml code automatically generated by VS. you can write it yourself to achieve the page layout you want.

@model MineLogin.Comment.User.LoginUser

@{
    ViewBag.Title = "Login";
}

<h2>Login</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>LoginUser</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PassWord, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PassWord, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PassWord, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Sign in" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

PS: since I am also Cpp and just transferred to C #, these things have been completed by self-study recently. If there is something wrong, I welcome your criticism and correction. The code sent here is the simplest login function without adding verification, special rendering and other technologies. I have also completed a relatively perfect login interface. If a little partner wants the source code, leave a message in the comment area or send a private letter!

Keywords: ASP.NET Windows mvc

Added by richie19rich77 on Mon, 20 Sep 2021 08:35:37 +0300