In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Briefly talk about MVC's Form authentication.
When doing the MVC project, when the user login authentication needs to choose Form authentication, what should we do? Let's give you a brief account.
First, let's talk about the steps.
1. When a user logs in, after verifying that the user name and password is passed, the method FormsAuthentication.SetAuthCookie () needs to be called.
2. When the user exits, you need to call FormsAuthentication.SignOut (); method
3. In the configuration file web.config, under the system.web node, configure
4. Check: HttpContext.User.Identity.IsAuthenticated. If it is false, it has not passed the authentication. If it is true, it has passed the authentication.
The above three parts can complete the Form authentication of user login.
All right, let's take a look at the specific code. (the code in View will not be posted, just the code in Controller.)
1. Set up a Model for user login
1 public class LoginViewModel2 {3 [DisplayName ("user name")] 4 public string UserName {get; set;} 5 [DisplayName (password)] 6 public string Password {get; set;} 7}
2. Set up a login Controller and page, in which there are two Action for login and login in Controller.
1 public class LoginController: Controller 2 {3 / / GET: Login 4 public ActionResult Index (LoginViewModel loginViewModel) 5 {6 if (loginViewModel.UserName = = "admin" & & loginViewModel.Password = = "123456") 7 {8 FormsAuthentication.SetAuthCookie (loginViewModel.UserName, false); 9 return RedirectToAction ("Index", "Main") 10} 11 return View (); 12} 13 14 / / GET: LogOut15 public ActionResult LogOut () 16 {17 FormsAuthentication.SignOut (); 18 return RedirectToAction ("Index", "Login"); 19} 20}
3. Create a page that users jump to after logging in with Controller
1 public class MainController: BaseController2 {3 / / GET: Main4 public ActionResult Index () 5 {6 return View (); 7} 8}
4. The Controller of the page that jumps after login is inherited from BaseController, so how does BaseController write?
1 public class BaseController: Controller 2 {3 protected override void OnActionExecuting (ActionExecutingContext filterContext) 4 {5 base.OnActionExecuting (filterContext); 6 / login authentication process 7 if (! filterContext.HttpContext.User.Identity.IsAuthenticated) 8 {9 / / not logged in 10 Response.Redirect ("~ / Login/Index") 11} 12 else13 {14 / / logged in, Action level privilege control processing 15 var controllerName = filterContext.RouteData.Values ["controller"] .ToString (); / / controller name 16 var actionName = filterContext.RouteData.Values ["action"] .ToString () / / Action name 17 / / permission check based on controllerName and actionName 18 / * 19 if () 20 {} 21 else22 {} 23 * / 24} 25} 26}
This BaseController is very simple, generally speaking, the function is to inherit the controller of the BaseController. When the Action below it is executed, the Form check will be performed. If the verification is successful, then... If the check is not successful, then
After login, the Controller of the page will inherit the BaseController, so that you don't have to write the Form authentication code repeatedly in the Action in each Controller.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.