[ASP. MVC4] Verifying user Login implementation

Source: Internet
Author: User

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
    1. Public ActionResult Index () {...}

Front Plus:

[CSharp]View Plaincopy
    1. [Authorize (Roles = "admins")]

That's it:

[CSharp]View Plaincopy
    1. [Authorize (Roles = "admins")]
    2. Public ActionResult Index ()
    3. {
    4. ...
    5. }

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
    1. <authentication mode="Forms" >
    2. <forms loginurl="~/login" timeout="2880"/>
    3. </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
  1. Public class Logonmodel
  2. {
  3. [Required]
  4. [Display (name = "user name")]
  5. public string UserName { get;  set;}
  6. [Required]
  7. [DataType (Datatype.password)]
  8. [Display (Name = "password")]
  9. public string Password { get;  set;}
  10. [Display (Name = "Next automatic Login")]
  11. public bool RememberMe { get;  set;}
  12. }

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
  1. @model Weibo.Models.LogOnModel //logonmodel is a class in the CDBTemplate.cs file
  2. @using (html.beginform ("Login","login", FormMethod.Post)) {
  3. @Html. textboxfor (M = m.username)
  4. @Html. validationmessagefor (M = m.username, "Please enter your username! ", new {style=" color: #f00 "})
  5. @Html. passwordfor (M = m.password)
  6. @Html. validationmessagefor (M = m.password,"Please enter your password! ",new {style=" color: #f00 "})
  7. @Html. checkboxfor (M = m.rememberme)
  8. @Html. labelfor (M = m.rememberme)
  9. @Html. ActionLink ("Forgot password", "forgotpwd", null, new {@class="RT", target="_blank"})
  10. <input type="Submit" value="Landing micro-blog"/>
  11. }

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
  1. [HttpPost, ActionName ("Login")]
  2. public void Login (FormCollection collection)
  3. {
  4. Object obj = sqlhelper.executescalar ("Select UserId from cdbusers where [email protected] and [email protected]",
  5. New SqlParameter ("@uname", Collection[0]),
  6. New SqlParameter ("@pwd", Weibo.Models.Myencrypt.myencrypt (collection[1)));
  7. if (obj! = null)
  8. {
  9. FormsAuthenticationTicket AuthTicket = new FormsAuthenticationTicket (
  10. 1,
  11. Collection[0],
  12. DateTime.Now,
  13. DateTime.Now.AddMinutes (30),
  14. false,
  15. "admins"
  16. );
  17. string encryptedticket = Formsauthentication.encrypt (AuthTicket);
  18. System.Web.HttpCookie Authcookie = new System.Web.HttpCookie (Formsauthentication.formscookiename,  Encryptedticket);
  19. SYSTEM.WEB.HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Authcookie);
  20. }
  21. Response.Redirect ("~/");
  22. }

[ASP. MVC4] Verifying user Login implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.