您现在的位置是:网站首页> 编程资料编程资料
MVC微信网页授权获取用户OpenId_实用技巧_
2023-05-24
201人已围观
简介 MVC微信网页授权获取用户OpenId_实用技巧_
最近开发微信公众平台,做下记录,以前也开发过,这次开发又给忘了,搞了半天,还是做个笔记为好。
注意框架为MVC 开发微信公众平台。场景为,在模板页中获取用户openid,想要进行验证的页面,集成模板页就可以了。
在_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); }
类AdminUtil中加入GetOpenID方法
#region 获取OpenID ////// 获取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
类WXApi中加入GetOpenID方法
#region 获取OpenId ////// 获取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/oauth2/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/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, screct, code)); } return Tools.GetJsonValue(strJson, "openid"); } #endregion
public static class WXModel { public static string access_token; public static string AppID; public static string AppSecret; } ////// 工具类 /// public class Tools { #region 获取Json字符串某节点的值 ////// 获取Json字符串某节点的值 /// 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) { //先截逗号,若是最后一个,截“}”号,取最小值 int end = jsonStr.IndexOf(',', index); if (end == -1) { end = jsonStr.IndexOf('}', index); } result = jsonStr.Substring(index, end - index); result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格 } } return result; } #endregion }
public class HttpRequestUtil { #region 请求Url,不发送数据 /// /// 请求Url,不发送数据 /// public static string RequestUrl(string url) { return RequestUrl(url, "POST"); } #endregion #region 请求Url,不发送数据 /// /// 请求Url,不发送数据 /// public static string RequestUrl(string url, string method) { // 设置参数 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"); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.Default); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } #endregion } 注意:需要在微信公众平台中设置授权回调域

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- ASP.NET MVC5+EF6+EasyUI后台管理系统 微信公众平台开发之资源环境准备_实用技巧_
- ASP.NET MVC5+EF6+EasyUI后台管理系统 微信公众平台开发之消息管理_实用技巧_
- ASP.NET 程序员都非常有用的85个工具_实用技巧_
- ASP.NET Core 数据保护(Data Protection 集群场景)下篇_实用技巧_
- ASP.NET Core 数据保护(Data Protection)中篇_实用技巧_
- ASP.NET Core 数据保护(Data Protection)上篇_实用技巧_
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)_实用技巧_
- .Net语言Smobiler开发利用Gridview控件设计较复杂的表单_实用技巧_
- .Net语言Smobiler开发之如何仿微信朋友圈的消息样式_实用技巧_
- ASP.NET MVC中URL地址传参的两种写法_实用技巧_
