Controller:
HomeController This is the controller of the homepage
Logincontroller, this is the login controller.
Class:
CDBTemplate.cs This is the database data corresponding class, which describes the structure of the database
First, the return function of the HomeController controller
[CSharp]View Plaincopy
- Public ActionResult Index () {...}
Front Plus:
[CSharp]View Plaincopy
- [Authorize (Roles = "admins")]
That's it:
[CSharp]View Plaincopy
- [Authorize (Roles = "admins")]
- Public ActionResult Index ()
- {
- ...
- }
This statement means to add a permission validation, allowing only user roles to be accessed by admins users
Then add it in the Web. config file:
[CSharp]View Plaincopy
- <authentication mode="Forms" >
- <forms loginurl="~/login" timeout="2880"/>
- </authentication>
This means adding user authentication to the entire site, and pointing to the login interface is the login controller
A class in the CDBTemplate.cs file:
[CSharp]View Plaincopy
- Public class Logonmodel
- {
- [Required]
- [Display (name = "user name")]
- public string UserName { get; set;}
- [Required]
- [DataType (Datatype.password)]
- [Display (Name = "password")]
- public string Password { get; set;}
- [Display (Name = "Next automatic Login")]
- public bool RememberMe { get; set;}
- }
Then add a view index.cshtml for the default return function of the Logincontroller controller, and add the following code to the page:
[CSharp]View Plaincopy
- @model Weibo.Models.LogOnModel //logonmodel is a class in the CDBTemplate.cs file
- @using (html.beginform ("Login","login", FormMethod.Post)) {
- @Html. textboxfor (M = m.username)
- @Html. validationmessagefor (M = m.username, "Please enter your username! ", new {style=" color: #f00 "})
- @Html. passwordfor (M = m.password)
- @Html. validationmessagefor (M = m.password,"Please enter your password! ",new {style=" color: #f00 "})
- @Html. checkboxfor (M = m.rememberme)
- @Html. labelfor (M = m.rememberme)
- @Html. ActionLink ("Forgot password", "forgotpwd", null, new {@class="RT", target="_blank"})
- <input type="Submit" value="Landing micro-blog"/>
- }
In the above code the first parameter of Html.BeginForm ("login", "login", formmethod.post) means the name of the method that specifies the controller to invoke, the second parameter means the name of the controller, The third parameter means how to submit the form to the server, where we choose to post it for security purposes.
Then add one of these methods to the Logincontroller controller:
[CSharp]View Plaincopy
- [HttpPost, ActionName ("Login")]
- public void Login (FormCollection collection)
- {
- Object obj = sqlhelper.executescalar ("Select UserId from cdbusers where [email protected] and [email protected]",
- 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 ("~/");
- }
[ASP. MVC4] Verifying user Login implementation