MVC4.0 Development of Password Modification module for developing CMS system case of ASP.

Source: Internet
Author: User

First for the last password login to do a supplementary note, we know that the Controller method summary using the filter [userauthorize] as a login verification is very convenient, verify the success, and then page jump, if the verification is unsuccessful I want to set them a jump page, We need to do the following configuration in config:

<authentication mode= "Forms" > <forms loginurl= "~/syscomuser/userlogin" timeout= "2880"/> </authenti Cation>

Modify the loginurl jump page.

User Login Password modification steps are as follows:

First, the definition of the template

    /// <summary>    ///  Change Password     /  </summary>    [NotMapped]    public class  syscomuserpassword    {        /// < summary>        ///  Original Password          /// </summary>        [display (Name= "Original password", description= "6-20 characters")]        [required (errormessage= "x")]         [stringlength (20,minimumlength=6,errormessage= "x")]         [datatype (Datatype.password)]         public string password { get; set; }         /// <summary>        ///  New Password         ///  </summary>        [display (name= "New password", Description= "6-20 characters")]         [required (errormessage= "x")]         [stringlength (20,minimumlength=6,errormessage= "x")]         [datatype (Datatype.password)]                public string NewPassword { get; set; }         /// <summary>        // /  Confirm Password         /// </summary>         [display (name =  "Confirm password", description =  "Enter password Again")]          [Compare ("NewPassword", errormessage =  "x")]        [ DataType (Datatype.password)]        public string  rnewpassword { get; set; }    }

     II, Contraller's action write

       /// <summary>         ///  Modify Password page         /// </summary>         /// <returns></returns>         [userauthorize]        public actionresult  userchangepassword ()         {             return view ();         }         /// <summary>         ///  Change Password Data         /// </summary>         /// <param name= "UserPassword" > Change password Entity Data </param >         /// <returns>url</returns>        [ httppost]        [userauthorize]// Extensionsh userauthorizeattribute Extension Rewrite Authorizecore (): Indicates that this is a URL request that only handles those authenticated URLs, and if the request is not authenticated, the action is taken to the login page.         public actionresult userchangepassword ( Syscomuserpassword userpassword)          {             int _rnum = userrpy.authentication ( Loginname, userpassword.password);             if  (_rnum == 0)             {                 //Read user information                   var _user = userrpy.find (LoginName);                 if  (_user == null)                  {                      Modelstate.addmodelerror ("Message",  "The user has expired, please login again!");                      return view ();                 }                 else                {                 &nBsp;   _user. password = userpassword.newpassword;                     if  (Userrpy.update (_user))                      {                          modelstate.addmodelerror ("Message",  "modified successfully!");                          return view ();                     }                     else                     {                          Modelstate.addmodelerror ("Message",  "failed to update the database!") ");                         return view ();                       }                 }             }             else            {                 modelstate.addmodelerroR ("Message",  "original password is incorrect, please re-enter!");                 return  View ();                }         }        public string  LoginName         {             get              {                 httpcookie _cookie = httpcontext.request.cookies["User"];                 if  (_cookie == null)  return   "";                 else return _cookie["LoginName"];             }        }

Add the public string LoginName property here, which is used primarily to read the login name from the cookie. Important methods are as follows:

1, verify the original login name and password is correct, although we [Userauthorize] did an authentication, but the goal here is to let the user re-enter the original password for verification, otherwise it is not allowed to modify:

Userrpy.authentication (LoginName, Userpassword.password);

2. Read all the information about the current login in the database:

var user = Userrpy.find (LoginName)

3. Assign the newly set password to the Read entity object and update it:

_user. Password = Userpassword.newpassword;
Userrpy.update (_user))

Third, the method definition of the business logic layer

1, modify a user information

        /// <summary>         ///  Modify a user's information         /// </summary>         /// <param name= "Tmodel" > User Data Model </param>         /// <returns> Boolean values </returns>         public override bool update (Syscomuser tmodel)          {            //if   (Tmodel == null)  { return false; }             //var _tmodel = hillstonecontext.syscomuser.firstordefault ( U => u.userid == tmodel.userid);             //if  (_tmodel == null)  { return false; }             //_tmodel = Tmodel;             //if  (Hillstonecontext.savechanges ()  > 0)              dbcontext.syscomuser.attach (Tmodel);             dbContext.Entry<SysComUser> (Tmodel). state = entitystate.modified;             if  (Dbcontext.savechanges ()  > 0)  return true;             else return false;         }

1), Syscomuser.attach (Tmodel);

Attaches the given entity to the underlying context of the set, which is used to repopulate the entities already in the database in the context, and Attach does nothing if the comparison to the entity in the database has not changed.

2), dbcontext.entry<syscomuser> (Tmodel). state = entitystate.modified;

A scalar property on the object has been modified, but the SaveChanges method has not been called. After you save the changes, the object state is changed to unchanged.

2, modify the user password, you need to read the user entity information from the data, the modified password to assign to this entity. The Read method is as follows:

<summary>///Find logged in User information///</summary>//<param name= "LoginName" > Login </param >//<returns> User Information </returns> public syscomuser Find (string LoginName) {VA            R _user = dbContext.SysComUser.SingleOrDefault (U = u.loginname = = LoginName);        return _user; }

3. Original Password Authentication method

        /// <summary>         ///  User logon authentication         /// </summary>         /// <param name= "LoginName" > Sign-in Name </param>         /// <param name= "password" > Password </param>         /// <returns>0: Login successful; 1: Login name does not exist; 2: Password error </returns>         public int authentication (string loginName,  String password)         {             var _user = dbcontext.syscomuser.singleordefault (u = > u.loginname == loginname);             if  (_user == null)  { return 1; }             if  (_user. Password != password)  { return 2; }             return 0;        }

      Four, view page code, automatically generated after making a simple adjustment

@model  Hillstone.Models.SysComUserPassword@{    ViewBag.Title =  "Change Password";     Layout =  "~/views/shared/_layout.cshtml";} 

This article is from the "Run small snail-original space" blog, please be sure to keep this source http://songjiahao.blog.51cto.com/4433831/1582907

MVC4.0 Development of Password Modification module for developing CMS system case of ASP.

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.