In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve MVC Wechat web page authorization to obtain user OpenId, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Note that the framework develops Wechat public platform for MVC. In the scenario, you can get the user openid in the template page and integrate the template page for the page you want to verify.
Add the following code to _ Layout.cshtml
@ ViewBag.Title-My ASP.NET Application @ Styles.Render ("~ / Content/css") @ Scripts.Render ("~ / bundles/modernizr") @ {var code = HttpContext.Current.Request ["code"]; Log.logmsg (code); string urlpath = HttpContext.Current.Request.Url.AbsoluteUri.ToString (); ViewBag.at = AdminUtil.GetOpenID (urlpath, code);}
Add GetOpenID method to class AdminUtil
# region get OpenID / public static string GetOpenID (string redirect_url, string code) {string AppID = WXModel.AppID; string AppSecret = WXModel.AppSecret; string openid = ""; openid = WXApi.GetOpenID (AppID, redirect_url, code, AppSecret); return openid;} # endregion
Add GetOpenID method to class WXApi
# region get OpenId / get OpenId / public static string GetOpenID (string appid, string redirect_url, string code, string screct) {string strJson = ""; if (string.IsNullOrEmpty (code)) {redirect_url = HttpUtility.UrlEncode (redirect_url) HttpContext.Current.Response.Redirect (string.Format ("https://open.weixin.qq.com/connect/oauth3/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect", appid, redirect_url, new Random (). Next (1000, 200000). ToString ()) } else {strJson = HttpRequestUtil.RequestUrl (string.Format ("https://api.weixin.qq.com/sns/oauth3/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, screct, code));} return Tools.GetJsonValue (strJson," openid ");} # endregionpublic static class WXModel {public static string access_token; public static string AppID Public static string AppSecret;} / tool class / public class Tools {# region get the value of a node in the Json string / public static string GetJsonValue (string jsonStr, string key) {string result = string.Empty If (! string.IsNullOrEmpty (jsonStr)) {key = "\" + key.Trim ('") +"\ "; int index = jsonStr.IndexOf (key) + key.Length + 1; if (index > key.Length + 1) {/ / truncated comma, if it is the last, truncate the"} "sign and take the minimum value int end = jsonStr.IndexOf (',', index) If (end = =-1) {end = jsonStr.IndexOf ('}', index);} result = jsonStr.Substring (index, end-index); result = result.Trim (new char [] {'",'','\'}); / / filter quotes or spaces}} return result } # endregion} public class HttpRequestUtil {# region request Url, no data / request Url, no data / public static string RequestUrl (string url) {return RequestUrl (url, "POST") } # endregion # region request Url, no data / request Url, no data / public static string RequestUrl (string url, string method) {/ / set parameter HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer (); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = method Request.ContentType = "text/html"; request.Headers.Add ("charset", "utf-8"); / / send the request and get the corresponding response data HttpWebResponse response = request.GetResponse () as HttpWebResponse; / / until the request.GetResponse () program starts sending Post requests to the target web page Stream responseStream = response.GetResponseStream (); StreamReader sr = new StreamReader (responseStream, Encoding.Default) / / return the result page (html) code string content = sr.ReadToEnd (); return content;} # endregion}
Note: the authorized callback domain needs to be set in Wechat public platform.
The above is all the content of this article "how to achieve MVC Wechat web page authorization to obtain user OpenId". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.