WeChat public platform development Session processing

Source: Internet
Author: User
Tags openid
In the window, the input information is limited. we need to divide some information into multiple requests. For example, when binding a user, you need to enter the user information, such as the user name, password, name, and phone number. after the server passes verification, you can bind the system user to the user. In the window, the input information is limited. we need to divide some information into multiple requests.

For example, when binding a user, you need to enter the user information, such as the user name, password, name, and phone number. after the server passes verification, you can bind the system user to the user.

Then, this account has certain functional permissions. you can check points and purchase records. Service number: China Merchants Bank credit card, there are many functions.

The client cannot cache information, and the input information is limited. you need to make multiple requests and save the current session status on the server. This requires Session.

This document takes user authentication and account binding as an example to describe how to deal with it.

1. create a general Session processing mechanism.

In order to better illustrate the principles and facilitate expansion, we can design our own sessions. Of course, you can also use System. Web. SessionState. HttpSessionState, which is a common Session mechanism for Web applications.

1. Custom Session

Used to store session fragments and related data.

Class Session {////// Cache hashtable ///Private static Hashtable mDic = new Hashtable ();////// Add //////Key///ValuePublic static void Add (string key, object value) {mDic [key] = value ;}////// Remove //////KeyPublic static void Remove (string key) {if (Contains (key) {mDic. Remove (key );}}////// Set the value /////////Public static void Set (string key, object value) {mDic [key] = value ;}////// Obtain the value /////////
 Public static object Get (string key) {return mDic [key];} ///// Whether it contains //////Key///
 
  
Bool
 Public static bool Contains (string key) {return mDic. ContainsKey (key );}////// Clear all items ///Public static void Clear () {mDic. Clear ();}}

2. operation type

Record the specific operation type to identify the specific operation of the current session

////// Operation type ///Enum Operation {////// Authentication ///Auth ,////// Add a user ///CreateUser}

3. operation process enumeration

Identifies the stage in which the current operation is processed.

////// Operation process ///Enum OperationStage {////// Default ///Default ,////// Step 1 ///First ,////// Step 2 ///Second ,////// Step 3 ///Third}

4. Session cache items

Items recorded in the cache, including the operation type, Operation steps, and session object. In order to facilitate Session management, the last access time is added to determine whether to automatically clear the identity.

Class SessionItem {////// Operation type ///Public Operation {get; set ;}////// Current step ///Public OperationStage {get; set ;}////// Data object ///Public object Data {get; set ;}////// Whether to automatically delete ///Public bool AutoRemove {get; set ;}////// Last update time ///Public DateTime UpdateTime {get; set ;}}

2. add Session processing to message processing. 1. add cache item data objects

This object records the information you enter during the session. It is also provided as an object for business processing data.

Class AuthSessionItem {////// User name ///Public string FromUserName {get; set ;}////// Account ///Public string Code {get; set ;}////// Unique identifier ///Public string ID {get; set ;}}

2. authentication process

1) start to enter the authentication, identify according to the authentication keyword, start the session, and cache relevant data

2) prompt for entering personal account information

3) the user enters the personal account, the server record the account information, and prompts to enter the employee card number.

4) the user enters the card number information, the server record the card number information, and calls the specific authentication logic

5) The user passes authentication and binds the OpenId. a message is displayed, indicating that the information is successfully bound and the session is cleared.

During the authentication process, the legality of user input information needs to be verified, and during the session process, the user can exit the current operation.

////// Authenticate user information /////////
 Private bool Auth (TextMessage tm, ref string response) {SessionItem sessionItem = null; if (string. equals (tm. content, "Auth", StringComparison. ordinalIgnoreCase) {// Check Whether authentication is performed. The business component verifies if (UserManager. isAuth (tm. fromUserName) {// If you have already authenticated, the message tm is displayed. content = "You have already passed the authentication and do not need to authenticate again! ";} Else {AuthSessionItem authSessionItem = new AuthSessionItem (); authSessionItem. fromUserName = tm. fromUserName; sessionItem. operator = Operation. auth; sessionItem. stage = OperationStage. first; sessionItem. data = authSessionItem; Session. set (tm. fromUserName, sessionItem); // enter the account and write the data and steps to the cache tm. content = "enter your personal account";} response = ResponseText (tm); return false;} // obtain user information from Session sessionItem = Sess Ion. Get (tm. FromUserName) as SessionItem; // if the session exists and the current operation is user-authenticated if (sessionItem! = Null & sessionItem. operator = Operation. auth) {if (sessionItem. stage = OperationStage. first) {tm. content = tm. content. trim (); if (string. isNullOrEmpty (tm. content) | tm. content. length> 20) {tm. content = "the entered personal account is invalid. please enter it again. "; Response = ResponseText (tm); return false;} AuthSessionItem authSessionItem = sessionItem. Data as AuthSessionItem; if (authSessionItem! = Null) {authSessionItem. code = tm. content;} // update the cache sessionItem. stage = OperationStage. second; Session. set (tm. fromUserName, sessionItem); tm. content = "enter your employee card number! \ N to Exit authentication, enter Exit. "; Response = ResponseText (tm);} else if (sessionItem. Stage = OperationStage. Second) {string cardNum = null; if (! Common. TryConvertToCardNum (tm. Content, out cardNum) {tm. Content = "the employee card number is invalid. please enter it again. \ N to Exit authentication, enter Exit. "; Response = ResponseText (tm); return false;} AuthSessionItem authSessionItem = sessionItem. Data as AuthSessionItem; if (authSessionItem! = Null) {authSessionItem. ID = cardNum;} // authentication string message; if (UserManager. authenticate (authSessionItem, out message) {tm. content = "congratulations, the authentication has been successful. you can use the address book query function. "; // Clear cache Session. Remove (tm. FromUserName); response = ResponseText (tm); return true;} else if (! String. IsNullOrEmpty (message) {tm. Content = message;} else {tm. Content = "Your input is incorrect. \ N re-authentication, enter Auth! ";}// Process ended: clear Session. Remove (tm. FromUserName); response = ResponseText (tm); return false ;}} return false ;}

3. exit the Session and clear the Session

During authentication, you can use commands to force the current operation to exit. when you exit the current operation, you need to clear the session information.

////// Exit and clear the Session ////////////
 Private bool Exit (TextMessage tm, ref string response) {// Exit if (string. equals (tm. content, "Exit", StringComparison. ordinalIgnoreCase) {// clears the Session. remove (tm. fromUserName); tm. content = "You have exited the current operation. please perform other operations. "; Response = ResponseText (tm); return true;} return false ;}

3. bind an account after user authentication is passed

The user passes authentication, binds the OpenId, and uses the OpenId to query the address book, personal points, and consumption records. User authentication is an identity authentication process and a user binding process. After the user identity authentication is passed, you can query the specific information through the account. At this time, the business layer can directly query User information based on the allocated OpenId.

IV. postscript

In this way, the public account can use a small text input box to implement more and more complex business applications. Of course, it is more intuitive and convenient to provide information input through web pages.

For more articles about Session processing on the public platform, please follow the PHP Chinese website!

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.