Xiongwen, which was seen in the blog Park News some time ago: Will you implement the Web user logon function?
Weak representation, accordingArticleStandards in, written for such a long timeProgram, It is really not completely written into the Web user login function, or the complex login function cannot be fully written according to this standard. Microsoft may be spoiled and deeply reflected.
Now let's get down to the point. Next we will focus on the discussion in this article: implementing basic user services such as registration and login Based on Forms authentication.
As we all know, Asp.net provides four authentication modes: "forms", "Windows", "Passport", and "NONE. In the Web. config file, we often see a configuration section like this:
< Authentication Mode = "Forms" > < Forms Cookieless = "Usecookies" Defaurl URL = "~ /Default. aspx" Loginurl = "~ /Login. aspx" Timeout = "2880" > </ Forms > </ Authentication >
The configuration file here is related to the forms authentication mode that will be discussed below.
To use forms authentication, usually with the above configurationCodeYou can call several classes such as formsauthentication to implement basic login registration, password modification, and other functions (how to call the MS class library to implement login registration and other functions is not the focus of this Article ).
You may ask how Ms implements the user registration and login function? Here, we can only tell you that it is related to membershipprovider. If you really want to know how the MS Logon component works, check its source code. I agree that Microsoft is doing too complicated and does not meet the actual business requirements. At least the companies I have worked for have not met the one implemented by Microsoft and used directly in the project.
Compare with the source code of membershipprovider, you can simply organize and abstract the following common interface methods:
Iuserservice Public Interface Iuserservice { /// <Summary> /// Create (Register) a user /// </Summary> /// <Param name = "userinfo"> </param> /// <Returns> </returns> Int Createuser (userinfo ); /// <Summary> /// Activate the user /// </Summary> /// <Param name = "userid"> </param> /// <Param name = "verifycode"> </param> /// <Returns> </returns> Int Activeuser ( Int Userid, String Verifycode ); /// <Summary> /// Log On (obtain user information based on the user name and password) /// </Summary> /// <Param name = "username"> </param> /// <Param name = "password"> </param> /// <Returns> </returns> Userinfo login ( String Username, String Password ); /// <Summary> /// Verify that the user is valid /// </Summary> /// <Param name = "Identity"> </param> /// <Returns> </returns> Bool Verifyuser (iidentity identity ); /// <Summary> /// Verify that the user is valid /// </Summary> /// <Param name = "userinfo"> </param> /// <Returns> </returns> Userinfo verifyuser (userinfo ); /// <Summary> /// Exit /// </Summary> Void Logout ();/// <Summary> /// Obtain the current logon user /// </Summary> /// <Returns> </returns> Userinfo getcurrentuser (); /// <Summary> /// Obtain a user based on the user ID /// </Summary> /// <Param name = "userid"> </param> /// <Returns> </returns> Userinfo getuserbyuserid ( Int Userid ); /// <Summary> /// Obtain a user based on the user name /// </Summary> /// <Param name = "username"> </param> /// <Returns> </returns> Userinfo getuserbyusername ( String Username ); /// <Summary> /// Obtain registered user information by email /// </Summary> /// <Param name = "email"> </param> /// <Returns> </returns> Userinfo getuserbyemail ( String Email );/// <Summary> /// Change the password /// </Summary> /// <Param name = "userid"> </param> /// <Param name = "stroldpwd"> </param> /// <Param name = "strnewpwd"> </param> /// <Returns> </returns> Int Resetpassword ( Int Userid, String Stroldpwd, String Strnewpwd ); /// <Summary> /// Update user information /// </Summary> /// <Param name = "userinfo"> </param> /// <Returns> </returns> Int Updateuser (userinfo ); /// <Summary> /// Delete (LOCK) the user /// </Summary> /// <Param name = "userid"> </param> /// <Returns> </returns> Int Deleteuser ( Int Userid );}
The importance of API-oriented programming is not explained.
Then, some interfaces naturally require specific implementation. The specific implementation is nothing more than specific processing according to specific rules:
Userservice Public Class Userservice: iuserservice { Public Userdao Dao { Get ; Set ;} Public Userservice () {Dao = New Userdao ();} Public Int Createuser (userinfo ){Return Dao. createuser (userinfo );} Public Int Activeuser ( Int Userid, String Verifycode ){ Return Dao. activeuser (userid, verifycode );} Public Userinfo login ( String Username, String Password) {userinfo = Dao. getuser (username, password ); If (Userinfo! =Null ) {Formsauthentication. setauthcookie (userinfo. username, False ); Userinfo. isauthenticated = httpcontext. Current. User. Identity. isauthenticated ;} Return Userinfo ;} Public Bool Verifyuser (iidentity identity ){ Bool Flag = False ; Userinfo = Dao. getuserbyusername (identity. Name ); If (Userinfo! = Null ) {Userinfo currentuser = verifyuser (userinfo ); If (Currentuser! = Null ) {Flag = True ;}} Return Flag ;} Public Userinfo verifyuser (userinfo ){ // Verify the user with custom rules String Verifycode = userutil. generateverifycode (userinfo. userid, userinfo. Email, userinfo. createdate ); Return Dao. verifyuser (userinfo, verifycode );} Public Void Logout () {formsauthentication. signout ();} Public Userinfo getcurrentuser () {userinfo result = Null ; If (Httpcontext. Current. User! = Null ) {Result = Dao. getuserbyusername (httpcontext. Current. User. Identity. Name );} Return Result ;} Public Userinfo getuserbyuserid (Int Userid ){ Return Dao. getuserbyuserid (userid );} Public Userinfo getuserbyusername ( String Username ){ Return Dao. getuserbyusername (username );} Public Userinfo getuserbyemail ( String Email ){ Return Dao. getuserbyemail (email );} Public Int Resetpassword (Int Userid, String Stroldpwd, String Strnewpwd ){ Return Dao. resetpassword (userid, stroldpwd, strnewpwd );} Public Int Updateuser (userinfo ){ Return Dao. updateuser (userinfo );} Public Int Deleteuser ( Int Userid ){ Return Dao. deleteuser (userid );}}
Userinfo is a custom object class of user information:
[Serializable] public class userinfo: iidentity, iprincipal {public int userid {Get; set;} Public String username {Get; set;} Public String password {Get; set ;} public int usertype {Get; set;} Public String email {Get; set;} public datetime createdate {Get; set;} public datetime updatedate {Get; set ;} public string name {Get; set;} Public String authenticationtype {get {return "forms" ;}} public bool isauthenticated {Get; set ;} public iidentity identity {get {return this ;}} public bool isinrole (string role) {return false ;}}
For a simple example, no other common attributes are listed.
The implementation of Dao will not be pasted. It is nothing more than accessing the database to perform some crud operations.
The success of such a simple and practical user service ...... Therefore, Ms programmers are happy on the premise that you have to be familiar with the principle and clear thinking.
Do you still find it difficult to log on to the web? Isn't it hard ?!
In fact, you can inherit the membershipprovider extension to implement your own membershipprovider, and also implement your own role permission management and other common user-related functions. A simple login function is a natural concept.
Of course, you can be unique and implement registration, login, security authentication, and other functions under custom complex rules, as described in the Xiongwen in the beginning of this article.