Home > Article > Backend Development > [Asp.Net MVC4] Verify user login implementation example
Recently we are going to make a Sina-like Weibo. It happens that I am learning MVC recently and want to use MVC technology to realize this project.
Since it is Weibo, you should know that there must be a user login, but it is different from the regular asp.NET login. The following is the research results of my afternoon + one night~~~
First of all , build the database and tables, it goes without saying.
Let’s talk about the main structure
Controller:
HomeController This is the controller of the homepage
LoginController This is the login controller
Class:
CDBTemplate.cs This is the database data The corresponding class describes the structure of the database
////////////////////////////////////// ////////I am the dividing line\\\\\\\\\\\\\\\\\\\\\\\\\\
First of all, in the return function of the HomeController controller
public ActionResult Index(){...}
Add in front:
[Authorize(Roles = "admins")]
That’s it:
[Authorize(Roles = "admins")] public ActionResult Index() { ... }
This statement means to add a permission verification here, allowing only users with the user role admins to access
Then add in the web.config file:
<authentication mode="Forms"> <forms loginUrl="~/Login" timeout="2880" /> </authentication>
These mean to add user verification to the entire website. The login interface pointed to is the login controller
A class in the CDBTemplate.cs file:
public class LogOnModel { [Required] [Display(Name = "用户名")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [Display(Name = "下次自动登陆")] public bool RememberMe { get; set; } }
Then add a view Index.cshtml to the default return function of the LoginController controller, and add the following code to the page:
@model Weibo.Models.LogOnModel //LogOnModel 是CDBTemplate.cs文件里的一个类 @using (Html.BeginForm("Login","Login",FormMethod.Post)) { @Html.TextBoxFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName, "请输入用户名!", new {style="color: #f00" }) @Html.PasswordFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password,"请输入密码!",new {style="color: #f00" }) @Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) @Html.ActionLink("忘记密码", "forgotpwd", null, new {@class="rt",target="_blank" }) <input type="submit" value="登陆微博" /> }
In the above code Html.BeginForm("Login", The first parameter of the "Login", FormMethod.Post) method means the name of the method that specifies the controller to be called, the second parameter means the name of the controller, and the third parameter means what method to use. Submit the form to the server. Here we choose to submit it in post mode for safety.
Then add such a method to the LoginController controller:
[HttpPost, ActionName("Login")] public void Login(FormCollection collection) { object obj = SqlHelper.ExecuteScalar("select UserId from CDBUsers where UserName=@uname and Password=@pwd", new SqlParameter("@uname", collection[0]), new SqlParameter("@pwd", Weibo.Models.Myencrypt.myencrypt(collection[1]))); if (obj != null) { FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, collection[0], DateTime.Now, DateTime.Now.AddMinutes(30), false, "admins" ); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); } Response.Redirect("~/"); }
Okay, done~~~~
The above is the entire content of this article, I hope it will be helpful to everyone’s learning, and I hope Please support PHP Chinese website.
For more [Asp.Net MVC4] verification user login implementation examples related articles, please pay attention to the PHP Chinese website!