国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

MVC微信網頁授權獲取用戶OpenId

2024-07-22 01:11:36
字體:
來源:轉載
供稿:網友

最近開發微信公眾平臺,做下記錄,以前也開發過,這次開發又給忘了,搞了半天,還是做個筆記為好。 

注意框架為MVC 開發微信公眾平臺。場景為,在模板頁中獲取用戶openid,想要進行驗證的頁面,集成模板頁就可以了。 

在_Layout.cshtml中加入如下代碼 

<head>  <meta charset="utf-8" />  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <title>@ViewBag.Title - My ASP.NET Application</title>  @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);  }</head> 

類AdminUtil中加入GetOpenID方法 

#region 獲取OpenID    /// <summary>    /// 獲取OpenID    /// </summary>    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    /// <summary>    /// 獲取OpenId    /// </summary>    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;  } 
 /// <summary>  /// 工具類  /// </summary>  public class Tools  {    #region 獲取Json字符串某節點的值    /// <summary>    /// 獲取Json字符串某節點的值    /// </summary>    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  }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南宫市| 葫芦岛市| 宜君县| 石家庄市| 工布江达县| 财经| 庆元县| 太原市| 岳阳县| 北海市| 芷江| 呼图壁县| 新乡县| 左权县| 临澧县| 瓮安县| 额尔古纳市| 南宫市| 孝义市| 苍南县| 舟山市| 渑池县| 巴东县| 克山县| 克什克腾旗| 治县。| 平乡县| 比如县| 卫辉市| 岐山县| 汝阳县| 丰都县| 轮台县| 大新县| 民和| 新余市| 焦作市| 津南区| 龙州县| 桐乡市| 申扎县|