.how does the official account of NET Wechat obtain OpenID and user information?
Editor to share with you. NET Wechat official account how to obtain OpenID and user information, I believe most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Index.aspx.cs Code:
Public partial class Index: System.Web.UI.Page {/ / user id public string openid = ""; / / official account information part public string appid = ConfigurationManager.AppSettings ["AppId"]; public string appsecret = ConfigurationManager.AppSettings ["AppSecret"]; public string redirect_uri = HttpUtility.UrlEncode ("http://www.jb51.net"); public string scope =" [delete this and enter the request type, for example: snsapi_userinfo] "; # region display page public string accesstoken; public string nickname Public string sex; public string headimgurl; public string province; public string country; public string language; public string city; public string privilege = ""; # endregion protected void Page_Load (object sender, EventArgs e) {/ * * Wechat certification acquisition openid part: * interim certification code * / / Wechat certification section: the second step is to obtain code string code = Request ["code"] If (string.IsNullOrEmpty (code)) {/ / if the code is not obtained successfully, pull the OpenAccess () again;} / / Wechat authentication section: the third step is to get openid string url = string.Format ("https://api.weixin.qq.com/sns/oauth3/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, appsecret, code); string result = HttpClientHelper.GetResponse (url)) LogHelper.WriteFile (result); JObject outputObj = JObject.Parse (result); / / Wechat authentication section: step 4: get more information accesstoken = outputObj ["access_token"] .ToString (); openid = outputObj ["openid"] .ToString (); url = string.Format ("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN",accesstoken,openid); string result1 = HttpClientHelper.GetResponse (url)) LogHelper.WriteFile (result1); JObject outputObj1 = JObject.Parse (result1); / / convert json to an array / / the following is the information obtained in step 4: nickname = outputObj1 ["nickname"]. ToString (); / / nickname sex = outputObj1 ["sex"]. ToString (); / / headimgurl = outputObj1 ["headimgurl"]. ToString (); / / profile url province = outputObj1 ["province"]. ToString (); Country = outputObj1 ["country"] .ToString (); language = outputObj1 ["language"] .ToString ();; city = outputObj1 ["city"] .ToString (); / / fill the obtained user information into session Session ["openid"] = outputObj1 ["openid"]; / / turn back to the entry / / OpenAccess () } / * * access entry * Open to the Wechat menu to call * @ param $dir_url Source url * @ since 1.0 * @ return void * / public void OpenAccess () {/ / determine that if does not exist in session (Session ["openid"] = = null) {/ / Authentication step 1: redirect to the authentication URL string url = string.Format ("https") : / / open.weixin.qq.com/connect/oauth3/authorize?appid= {0} & redirect_uri= {1} & & response_type=code&scope=snsapi_userinfo&m=oauth3#wechat_redirect " Appid, redirect_uri) Response.Redirect (url);} / determine the existence of else in session {/ / jump to the front page .aspx Response.Redirect (Request.Url.ToString ());}
Index.aspx content:
Td {word-wrap: break-word;}
Openid:
Nickname:
Sex:
Language:
City:
Country:
Headimgurl:
Privilege:
HttpClientHelper.cs Code:
Public class HttpClientHelper {/ get request / public static string GetResponse (string url) {if (url.StartsWith ("https")) {ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;} var httpClient = new HttpClient (); httpClient.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue ("application/json")); HttpResponseMessage response = httpClient.GetAsync (url). Result If (response.IsSuccessStatusCode) {string result = response.Content.ReadAsStringAsync (). Result; return result;} return null;} public static T GetResponse (string url) where T: class, new () {if (url.StartsWith ("https")) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; var httpClient = new HttpClient (); httpClient.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue ("application/json")); HttpResponseMessage response = httpClient.GetAsync (url). Result T result = default (T); if (response.IsSuccessStatusCode) {Task t = response.Content.ReadAsStringAsync (); string s = t.Result; result = JsonConvert.DeserializeObject (s);} return result;} / post request / post data / public static string PostResponse (string url, string postData) {if (url.StartsWith ("https")) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls HttpContent httpContent = new StringContent (postData); httpContent.Headers.ContentType = new MediaTypeHeaderValue ("application/json"); var httpClient = new HttpClient (); HttpResponseMessage response = httpClient.PostAsync (url, httpContent) .result; if (response.IsSuccessStatusCode) {string result = response.Content.ReadAsStringAsync () .Result; return result;} return null } / url / post data / public static T PostResponse (string url, string postData) where T: class, new () {if (url.StartsWith ("https")) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpContent httpContent = new StringContent (postData); httpContent.Headers.ContentType = new MediaTypeHeaderValue ("application/json"); var httpClient = new HttpClient () T result = default (T); HttpResponseMessage response = httpClient.PostAsync (url, httpContent) .Result; if (response.IsSuccessStatusCode) {Task t = response.Content.ReadAsStringAsync (); string s = t.Result; result = JsonConvert.DeserializeObject (s);} return result } / V3 interfaces are all in Xml form, so there is this method / public static T PostXmlResponse (string url, string xmlString) where T: class, new () {if (url.StartsWith ("https")) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpContent httpContent = new StringContent (xmlString); httpContent.Headers.ContentType = new MediaTypeHeaderValue ("application/json"); var httpClient = new HttpClient () T result = default (T); HttpResponseMessage response = httpClient.PostAsync (url, httpContent) .Result; if (response.IsSuccessStatusCode) {Task t = response.Content.ReadAsStringAsync (); string s = t.Result; result = XmlDeserialize (s);} return result } / public static T XmlDeserialize (string xmlString) where T: class, new () {try {var ser = new XmlSerializer (typeof (T)); using (var reader = new StringReader (xmlString)) {return (T) ser.Deserialize (reader) }} catch (Exception ex) {throw new Exception ("XmlDeserialize exception: xmlString:" + xmlString + "exception message:" + ex.Message);}
The result is shown in the figure:
The above is all the content of this article entitled "how to get OpenID and user Information on .NET Wechat official account". 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!