Simply talk about MVC's form certification.
When doing MVC project, user login authentication need to choose form authentication, what should we do? Let's give you a brief talk.
Let's start with the steps.
1, when the user logs on, if the user name password verification after passing, you need to call Formsauthentication.setauthcookie () this method.
2, when the user exits, you need to call FormsAuthentication.SignOut ();
3. In configuration file Web. config, under the system.web node, configure <authentication mode= "Forms"/>
4, check: HttpContext.User.Identity.IsAuthenticated, if False, there is no authentication, if true, then passed the authentication
Above these three departments, you can complete the user login form certification.
OK, let's take a look at the specific code. (The code in the view is not posted, only the code in the Controller)
1. Establish a model for user login
1 Public classLoginviewmodel2 {3[DisplayName ("User name")]4 Public stringUserName {Get;Set; }5[DisplayName ("Password")]6 Public stringPassword {Get;Set; }7}
2, the establishment of login with the controller and page, where controller has login and exit two action
1 Public classLogincontroller:controller2 {3 //Get:login4 Publicactionresult Index (Loginviewmodel loginviewmodel)5 {6 if(Loginviewmodel.username = ="Admin"&& Loginviewmodel.password = ="123456")7 {8Formsauthentication.setauthcookie (Loginviewmodel.username,false);9 returnRedirecttoaction ("Index","Main");Ten } One returnView (); A } - - //Get:logout the Publicactionresult LogOut () - { - formsauthentication.signout (); - returnRedirecttoaction ("Index","Login"); + } -}
3, the establishment of a login, users jump page and controller
1 public class Maincontroller:basecontroller 2 { 3 // Get:main 4 public ActionResult Index () 5 Span style= "color: #000000;" > { 6 return View (); 7 8
}
4, landing after the page to jump controller is inherited Basecontroller, then Basecontroller is how to write it?
1 Public classBasecontroller:controller2 {3 protected Override voidonactionexecuting (actionexecutingcontext filtercontext)4 {5 Base. OnActionExecuting (filtercontext);6 //Login Authentication Processing7 if(!filterContext.HttpContext.User.Identity.IsAuthenticated)8 {9 //not logged inTenResponse.Redirect ("~/login/index"); One } A Else - { - //logged in, action-level permission control processing the varControllername = filtercontext.routedata.values["Controller"]. ToString ();//Controller name - varActionName = filtercontext.routedata.values["Action"]. ToString ();//Action name - //permission checks based on Controllername and ActionName - /* + if () - { } + Else A { } at */ - } - } -}
This basecontroller is very simple, the general function is that the way to inherit the Basecontroller controller, when executing its action below, will be a form check, if the verification is successful, then ..., if the verification is not successful ...,
The controller of the landing page will inherit the Basecontroller so that it does not have to repeat the code of the form authentication in the action of each controller.
Isn't it simple?
Of course, the specific details of the issue is not involved here, here is simply to introduce you to the use of form certification, specific details of the problem, we can refer to the garden of the Great Gods of the blog.
On MVC form Authentication