Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write ASP.NET MVC5 website developer user login and logout

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to write ASP.NET MVC5 website development user login and logout". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, create a ClaimsIdentity

ClaimsIdentity (delegate based on declaration identity) is used when the ASP.NET Identity authentication system logs in, and we generate it in UserService.

1. Open the InterfaceUserService API of IBLL project, and add the interface method ClaimsIdentity CreateIdentity (User user, string authenticationType).

2. Open the UserService class of the BLL project and add the implementation code of the CreateIdentity method

Public ClaimsIdentity CreateIdentity (User user, string authenticationType) {ClaimsIdentity _ identity = new ClaimsIdentity (DefaultAuthenticationTypes.ApplicationCookie); _ identity.AddClaim (new Claim (ClaimTypes.Name, user.UserName)); _ identity.AddClaim (new Claim (ClaimTypes.NameIdentifier, user.UserID.ToString (); _ identity.AddClaim (new Claim ("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider"," ASP.NET Identity ")); _ identity.AddClaim (new Claim (" DisplayName ", user.DisplayName)); return _ identity;}

2. Get AuthenticationManager (Authentication Manager)

Open the UserController in the Member area of the Ninesky.Web project, add the AuthenticationManager property, and get it in HttpContext.GetOwinContext ().

# region attribute private IAuthenticationManager AuthenticationManager {get {return HttpContext.GetOwinContext (). Authentication;}} # endregion

Third, create a login view model

Model folder in the Member area to add a view model

Using System.ComponentModel.DataAnnotations Namespace Ninesky.Web.Areas.Member.Models {/ login model / public class LoginViewModel {/ user name / [Required (ErrorMessage = "required")] [StringLength (20, MinimumLength = 4, ErrorMessage = "{2} to {1} characters")] [Display (Name = "user name")] public string UserName {get; set } / password / / [Required (ErrorMessage = "required")] [Display (Name = "password")] [StringLength (20, MinimumLength = 6, ErrorMessage = "{2} to {1} characters")] [DataType (DataType.Password)] public string Password {get; set;} / remember me / [Display (Name = "remember me")] public bool RememberMe {get; set;}

Create a login page

Add (string returnUrl) action to UserCcontroller

/ user login / return Url / public ActionResult Login (string returnUrl) {return View ();}

Right-click to add the enhanced type view, and the model is LoginViewModel

@ model Ninesky.Web.Areas.Member.Models.LoginViewModel@ {ViewBag.Title = "member login" } @ using (Html.BeginForm ()) {@ Html.AntiForgeryToken () member login @ Html.ValidationSummary (true) @ Html.LabelFor (model = > model.UserName, new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.UserName) @ Html.ValidationMessageFor (model = > model.UserName) @ Html.LabelFor (model = > model.Password) New {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Password) @ Html.ValidationMessageFor (model = > model.Password) @ Html.LabelFor (model = > model.RememberMe, new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.RememberMe) @ Html.ValidationMessageFor (model = > model.RememberMe) @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval")}

Effect.

Create a user login to process action

In the Login action that adds httppost type to UserCcontroller, use ModelState.IsValid to see if the model is verified, and if it fails to return directly, check whether the user's password is correct. The username and password correctly use the CreateIdentity method to create the identity, then use the SignOut method to empty the Cookies, and then log in with SignIn.

[ValidateAntiForgeryToken] [HttpPost] public ActionResult Login (LoginViewModel loginViewModel) {if (ModelState.IsValid) {var _ user = userService.Find (loginViewModel.UserName); if (_ user = = null) ModelState.AddModelError ("UserName", "user name does not exist"); else if (_ user.Password = = Common.Security.Sha256 (loginViewModel.Password)) {var _ identity = userService.CreateIdentity (_ user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignOut (DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn (new AuthenticationProperties () {IsPersistent = loginViewModel.RememberMe}, _ identity) Return RedirectToAction ("Index", "Home");} else ModelState.AddModelError ("Password", "wrong password");} return View ();}

VI. Modify the user registration code

Allow users to log in directly after successful registration

VII. Cancellation

Add to UserCcontroller, add to Logout action

/ logout / public ActionResult Logout () {AuthenticationManager.SignOut (DefaultAuthenticationTypes.ApplicationCookie); return Redirect (Url.Content ("~ /"));} "how to write ASP.NET MVC5 website development user login, logout" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report