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 get the Openid of current users in the official account development web page of Wechat

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

Share

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

This article mainly introduces how to obtain the current user Openid in the Wechat official account development web page, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.

Concrete realization

First, we decide on a method to get openid, ReGetOpenId.

Public static void ReGetOpenId () {string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri / / get the current url if (System.Web.HttpContext.Current.Session ["openid"] = "" | | System.Web.HttpContext.Current.Session ["openid"] = = null) {/ / first determine whether it is the if jumped over after obtaining the code (System.Web.HttpContext.Current.Request.QueryString ["code"] = "" | | System.Web.HttpContext.Current. Request.QueryString ["code"] = = null) {/ / Code is empty Get Code string GetCodeUrls = GetCodeUrl (url) first System.Web.HttpContext.Current.Response.Redirect (GetCodeUrls) / / first jump to Wechat's server. After obtaining the code, the} else {/ / Code of this page will jump back. You have already obtained the code and jumped back. Now get openid Log log = new Log (AppDomain.CurrentDomain.BaseDirectory + @ "/ log/Log.txt"). String openid = ""; openid = GetOauthAccessOpenId (System.Web.HttpContext.Current.Request.QueryString ["Code"]); / / retrieve the user's openid System.Web.HttpContext.Current.Session ["openid"] = openid;}

Note: url had better have a domain name, but the domain name of peanut shell will not work. When you adjust the Wechat platform interface, you will report an incorrect link.

The GetCodeUrl method above is as follows

# region re-obtain the jump link of Code (only basic information can be obtained without user authorization) / / re-obtain Code to jump back to the target page with Code (without user authorization, only basic information can be obtained (openid)) / / target page / public static string GetCodeUrl (string url) {string CodeUrl = "" / / Encoding a pair of url url = System.Web.HttpUtility.UrlEncode (url); CodeUrl = string.Format ("https://open.weixin.qq.com/connect/oauth3/authorize?appid=" + Appid +" & redirect_uri= "+ url +"? action=viewtest&response_type=code&scope=snsapi_base&state=1#wechat_redirect "); return CodeUrl;} # endregion

The GetOauthAccessOpenId method above is as follows

# region exchanges Code for user's openid, access_token / obtains user's openid and access_token public static string GetOauthAccessOpenId (string code) {Log log = new Log (AppDomain.CurrentDomain.BaseDirectory + @ "/ log/Log.txt") according to Code; string Openid = "" String url = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=" + Appid +" & secret= "+ Secret +" & code= "+ code +" & grant_type=authorization_code "; string gethtml = MyHttpHelper.HttpGet (url); log.log (" url obtained is: "+ url); log.log (" gethtml obtained is "+ gethtml) OAuth_Token ac = new OAuth_Token (); ac = JsonHelper.ToObject (gethtml); log.log ("can I get openid= from html" + ac.openid); Openid = ac.openid; return Openid;} # endregion

You can get the user's Openid by using the above method. As shown above, the user's id is saved in System.Web.HttpContext.Current.Session ["openid"], so it is very easy to get it.

Execute where you need to get

# region gets the current user Openid ReGetOpenId (); log.log ("after walking through the method of obtaining openid, the value of the current Session is" + System.Web.HttpContext.Current.Session ["openid"]); # endregion

Note: the OAuth_Token class above is as follows:

Public class OAuth_Token {/ Web page authorization API calls credentials. Note: this access_token is different from the basic supported access_token. / public string access_token {get; set;} / access_token API call credential timeout (in seconds) / public string expires_in {get; set } / the user refreshes the access_token / public string refresh_token {get; set;} / user's unique identity. Please note that when the user visits the web page of the official account without following the official account, it will also generate a unique OpenID / public string openid {get; set of the user and the official account. } / user authorization scope / public string scope {get; set;}}

Log file

The simple log class used is also provided and put up by the way:

/ public class Log {private string logFile; private StreamWriter writer; private FileStream fileStream = null; public Log (string fileName) {logFile = fileName; CreateDirectory (logFile) } public void log (string info) {try {System.IO.FileInfo fileInfo = new System.IO.FileInfo (logFile); if (! fileInfo.Exists) {fileStream = fileInfo.Create (); writer = new StreamWriter (fileStream) } else {fileStream = fileInfo.Open (FileMode.Append, FileAccess.Write); writer = new StreamWriter (fileStream);} writer.WriteLine (DateTime.Now + ":" + info) } finally {if (writer! = null) {writer.Close (); writer.Dispose (); fileStream.Close (); fileStream.Dispose () } public void CreateDirectory (string infoPath) {DirectoryInfo directoryInfo = Directory.GetParent (infoPath); if (! directoryInfo.Exists) {directoryInfo.Create ();}

As for the invocation, it is very simple. The calling method is as follows:

Log log = new Log (AppDomain.CurrentDomain.BaseDirectory + @ "/ log/Log.txt"); log.log ("I will be entered in the log file")

Finally, if you get the Openid of the current user, you can get other basic information of the user from the database again. This can better assist you to complete other business modules in your project.

Thank you for reading this article carefully. I hope the article "how to get the current user Openid in the official account development page of Wechat" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

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