Precautions for ASP. NET System Integration discuznt3.6

Source: Internet
Author: User

Recently, an ASP. NET system needs to be integrated with discuz to implement two-way login, registration, and password modification.

I checked some information and finally chose the officialDiscuztoolkitBecause it is open-source, it is easy to use. You can make some adjustments as needed. Thanks to the DZ team.

The first step is to install and debug the Forum. Everything is normal.

Step 2: learn the official API documentation at http://nt.discuz.net/showtopic-66493.aspx.

Step 3: view the official sample documentation at http://nt.discuz.net/showtopic-81187.aspx.

Step 4: Introduce discuz in the project. toolkit. DLL (you can directly add an open-source project to the Project) adds a synchronous logon or BBS account registration method to the website. After logging on or registration is completed, the method is as follows:

View code

 //  ... LoginCode.  //  Synchronize login information to the Forum. If you do not have this Member, register  Try  {Discuzsession DS = Discuzsessionhelper. getsession (); discuz. toolkit. User user = Null  ;  Try  {User = DS. getloggedinuser (). getuserinfo ();}  Catch  {}  If (User = Null | User. Username! = _ User. username ){  Int Uid =DS. getuserid (_ User. username );  If (UID < 1  ){  //  Register a new user Uid = Ds. Register (_ User. username, password, _ User. email, True  );  //  Fill in Basic Information Userforediting UFE = New  Userforediting (); UFE. realname =_ User. actualname; UFE. idcard = _ User. idcard; UFE. Mobile = _ User. Phone; UFE. Phone = _ User. Tel; UFE. Gender = _ User. Gender. tostring (); UFE. Nickname = _ User. actualname; UFE. groupid = 10 ; //  Membership level. Special Groups may be used for some other applications.  DS. setuserinfo (UID, UFE );}  // Synchronous Logon DS. login (UID, password, True , 0 , ""  );}}  Catch  (Exception ex) {loghelper. writelog (Ex );  //  Error Log }
 
The synchronization method is as follows:
View code

Discuzsession DS = Discuzsessionhelper. getsession ();  Int Uid =0  ;  Try  {  //  User = Ds. getloggedinuser (). getuserinfo (); Uid = DS. getuserid (oldm. username );  If (UID> 0  ){  //  Todo: the relationship between discuz. Web. Services. Actions. Users and discuz. toolkit. user must be the same as userforediting.  //  Fill in Basic Information Userforediting UFE = New  Userforediting (); UFE. realname = Model. actualname; UFE. idcard = Model. idcard; UFE. Mobile = Model. Phone; UFE. Phone = Model. Tel; UFE. Gender = Model. Gender. hasvalue? Model. Gender. tostring (): "  0  "  ; UFE. Nickname = Model. actualname; UFE. Password = Model. Password; UFE. groupid = Model. level. Value + 16  ; UFE. Username = Model. Username; UFE. Email = Model. Email; UFE. Website = ""  ; UFE. Location = ""  ; DS. setuserinfo (UID, UFE );}}  Catch  (Exception ex) {loghelper. writelog (Ex );} 

The above groupid and username attributes are not available in the official open-source project userforediting class. You can manually add these attributes and pay attention to discuz. web. services. actions. users and discuz. toolkit. the user must correspond to the same userforediting as follows:

View code

 //  Added the following to the userforediting class under discuz. Web. Services. API:  [Jsonpropertyattribute (  "  Group_id  "  )]  Public   Int ? Groupid; //  User Group ID [Jsonpropertyattribute (  "  User_name  "  )]  Public   String Username; //  User Name  //  In discuz. Web. Services. API. Actions, add the following to the setinfo () method of the users class:  //  Group  If  (UFE. groupid. hasvalue) {userinfo. groupid =UFE. groupid. value ;}  //  User Name  If (UFE. Username! = Null & UFE. Username! = ""  ) {Userinfo. Username = UFE. Username ;} 

 If the class attributes in services are inconsistent with those previously called, a serialization exception occurs.

So far, we have implemented the function of synchronizing logon, registration, and modification of basic information from the website to the Forum.

 
The next step is to implement synchronous login, registration, and modification from the Forum to the website. Here we only use logon as an example. The other steps are similar. 
 
In this part, we will use the official API for operations.

Step 1: add the call URL in "extension"-"integration Settings" in the background,

In the preceding figure, the URL of the data to be synchronized is a processing page on the website.

Second, to understand the parameter type, DZ's synchronization mechanism is to transmit parameters to the specified URL address through JavaScript scripts after login, registration, and other actions on the forum, the basic format of the parameter is uid = 1 & user_name = Test & time = 1270283913981 & Action = login & Sig = dskljoiwjelkjskdjfoiwelkjsifs. You can learn about the functions of these parameters in the official API descriptions, especially the generation of Sig.

Step 3: understand the values and parameters of various actions in the discuz. Forum. Sync class to facilitate processing on the website's API page.

Next, we will implement login synchronization as follows:

View code

 Private  Discuzsession Ds;  Private   String Action = ""  , User_name, password; Private   Int Uid = 0  ;  Protected   Void Page_load ( Object  Sender, eventargs e ){  Try  {DS = Discuzsessionhelper. getsession ();  If (! Ispostback) {Action = Handler. requestquerystring ( " Action  "  ); Uid = Handler. requestqueryint ( "  UID  "  ); User_name = Handler. requestquerystring ( "  User_name  "  ); Password = Handler. requestquerystring ( "  Password  " );  If (Action! = "" & Check_sig ()) //  Verify if SIG is valid  {  Switch  (Action ){  Case   "  Login  " : //  Synchronous Logon: uid, user_name, time, and Action + secret (key) MD5 + calculate SIG Login ();  Break  ;  Case   "  Logout  " : //  Sync logout  Loginout ();  Break  ;  Case   "  Updatepwd " : //  Change Password synchronously                          Break  ;}}}}  Catch  (Exception ex) {loghelper. writelog (Ex );}}  ///   <Summary>  ///  Login  ///   </Summary>  Private   Void Login () {discuz. toolkit. User user = DS. getuserinfo (UID );  If (User! = Null  ) Memberop. validatelogin (user. username, user. Password,  True  );}  ///   <Summary>  ///  Exit  ///   </Summary>  Private   Void Loginout () {memberop. loginout (  False  );} 

 In the above check_sig () method, it is mainly to verify whether the SIG is correct. This value is MD5 after the parameter is sorted. The specific process can be referred to: discuz. forum. sync. geturl ().

 
Note that the apikey, secret, and URL values must be correctly configured. Now, you have synchronized login and exit from the Forum to the website. 

Discuztoolkit thread synchronization, I believe these two problems will certainly encounter:

(1) When registering a user, the following exception occurs: The call_id submitted by the current session is not greater than the previous call_id

(2) An item with the same key has already been added.

Solution: http://www.cnblogs.com/baihmpgy/archive/2012/04/09/2438720.html

In the solution, there is a thread lock problem:

Lock (_ syncroot) can be used directly with lock (this)

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.