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 create a general Session processing mechanism for the development value of Wechat public platform

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you the Wechat public platform development value of how to create a general Session processing mechanism, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

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, enumerate the operation process

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 entry

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;} / the last update time / public DateTime UpdateTime {get; set;}} 2. Add Session processing to the 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. Business components verify that if (UserManager.IsAuth (tm.FromUserName)) {/ / if so, prompt tm.Content = "you have been certified. There is no need for re-certification! " ;} 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 from Session sessionItem = Session.Get (tm.FromUserName) as SessionItem / / 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 re-enter it. \ nPlease enter Exit to exit the authentication. " ; 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, you have been verified successfully, you 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 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 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;} III. User is verified and bound to 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.

These are all the contents of the article "how to create a general Session processing mechanism for Wechat public platform development values". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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