Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with the Session problem in the Development of Wechat Public platform

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces how to deal with the Session problem in the development of Wechat public platform. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

In the Wechat window, the input information is limited, we need to request some information multiple times.

For example: in the user binding, we need to enter the user's relevant information, such as: user name, password, or name, phone number, server verification, we can bind the system user with Wechat users.

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

Wechat client can not cache information, and the input information is limited, so it needs to make multiple requests to save the current session state on the server side. This requires Session.

First, create a general Session processing mechanism.

In order to better explain the principle and facilitate expansion, we design the Session ourselves. Of course, System.Web.SessionState.HttpSessionState can also be used here, which is the common Session mechanism of Web.

1. Custom Session

Used to store session fragments and related data.

Class Session {/ cache hashtable / private static Hashtable mDic = new Hashtable (); / key / value public static void Add (string key, object value) {mDic [key] = value } / remove / key public 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 } / get the value / public static object Get (string key) {return mDic [key];} / 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 and identify the specific operation of the current session

/ Operation type / enum Operation {/ Authentication / Auth, / add user / CreateUser}

3. Enumeration of operation procedures

It is used to identify the current operation, which stage it is in, and different stages to do different processing.

/ Operation procedure / enum OperationStage {/ default / / Default, / first step / First, / second step / Second, / third step / Third}

4. Session cache entries

Cache the entry of the record, which records the operation type, procedure, and session object. In order to facilitate Session management, also increased the last access time, whether to automatically clear the identity.

Class SessionItem {/ Operation Type / public Operation Oper {get; set;} / current step / public OperationStage Stage {get; set;} / data object / public object Data {get; set } / whether to automatically delete / public bool AutoRemove {get; set;} / last updated time / public DateTime UpdateTime {get; set;}}

Second, it is necessary to add Session processing to message processing.

1. Add cache item data object

This object records the relevant information entered by the user during the session. It also provides objects as business processing data.

Class AuthSessionItem {/ username / public string FromUserName {get; set;} / account / public string Code {get; set;} / unique ID / public string ID {get; set;}}

2. Authentication process

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

2) prompt to enter personal account information

3) Wechat users enter personal accounts, the server records the account information, and prompts the employee card number

4) Wechat users enter the card number information, the server records the card number information, and calls the specific authentication logic

5) the user is authenticated, bind Wechat OpenId, prompt for successful binding information, and clear the session.

In the authentication process, it is necessary to verify the legitimacy of the user input information, and in the process of the session, the user is supported to exit the current operation.

/ authenticated user information / private bool Auth (TextMessage tm, ref string response) {SessionItem sessionItem = null If (string.Equals (tm.Content, "Auth", StringComparison.OrdinalIgnoreCase)) {/ / check whether it has been authenticated, and the business component verifies that if (UserManager.IsAuth (tm.FromUserName)) {/ / if it has been certified, prompts tm.Content = "you have been certified, you do not need to re-authenticate!" Else {AuthSessionItem authSessionItem = new AuthSessionItem (); authSessionItem.FromUserName = tm.FromUserName; sessionItem.Oper = Operation.Auth; sessionItem.Stage = OperationStage.First; sessionItem.Data = authSessionItem; Session.Set (tm.FromUserName, sessionItem) / / enter the account number and write the data and steps to the cache tm.Content = "Please enter your personal account number";} response = ResponseText (tm); return false;} / / get user information sessionItem = Session.Get (tm.FromUserName) as SessionItem from Session / / if the session exists and the current operation is user authentication if (sessionItem! = null & & sessionItem.Oper = = Operation.Auth) {if (sessionItem.Stage = = OperationStage.First) {tm.Content = tm.Content.Trim () If (string.IsNullOrEmpty (tm.Content) | | tm.Content.Length > 20) {tm.Content = "the personal account entered 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 cache sessionItem.Stage = OperationStage.Second; Session.Set (tm.FromUserName, sessionItem) Tm.Content = "Please enter your employee card number!" \ nPlease enter Exit to exit the authentication. " ; response = ResponseText (tm);} else if (sessionItem.Stage = = OperationStage.Second) {string cardNum = null; if (! Common.TryConvertToCardNum (tm.Content, out cardNum)) {tm.Content = "employee card number is invalid, please enter it again.\ nPlease enter Exit to exit the authentication." ; response = ResponseText (tm); return false;} AuthSessionItem authSessionItem = sessionItem.Data as AuthSessionItem; if (authSessionItem! = null) {authSessionItem.ID = cardNum;} / / Certification string message If (UserManager.Authenticate (authSessionItem, out message)) {tm.Content = "Congratulations, you have been successfully authenticated and can use the query function of the address book." ; / / Clean the cache Session.Remove (tm.FromUserName); response = ResponseText (tm); return true;} else if (! string.IsNullOrEmpty (message)) {tm.Content = message;} else {tm.Content = "the information you entered is incorrect. \ nPlease enter Auth! ";} / / end of process: clean up Session Session.Remove (tm.FromUserName); response = ResponseText (tm); return false;}} return false;}

3. Exit the session and clean up the Session

During the authentication process, the user can forcibly exit the current operation through the command, and the session information needs to be cleaned up when exiting the current operation.

/ exit and clean up Session / private bool Exit (TextMessage tm, ref string response) {/ / exit if (string.Equals (tm.Content, "Exit", StringComparison.OrdinalIgnoreCase)) {/ / clear Session Session.Remove (tm.FromUserName) Tm.Content = "you have exited the current operation, please do something else." ; response = ResponseText (tm); return true;} return false;}

Third, the user is authenticated and bound to the Wechat account

User authentication passed, and bound Wechat OpenId, through the OpenId can query address book, query personal points and consumption records and other operations. User authentication is not only an identity authentication process, but also a user binding process. After the user identity authentication is passed, the specific information can be queried through the Wechat account. At this time, the business layer can directly query user information according to the OpenId assigned by Wechat.

In this way, public accounts can achieve more and more complex business applications through small text input boxes. Of course, it is more intuitive and convenient to input information by providing web pages.

So much for sharing about how to deal with Session problems in the development of Wechat public platform. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report