欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何實現(xiàn)MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId-創(chuàng)新互聯(lián)

小編給大家分享一下如何實現(xiàn)MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)廈門免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

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


在_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/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");
    }
    #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字符串某節(jié)點的值
    /// <summary>
    /// 獲取Json字符串某節(jié)點的值
    /// </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

  }
public class HttpRequestUtil
  {
    #region 請求Url,不發(fā)送數(shù)據(jù)
    /// <summary>
    /// 請求Url,不發(fā)送數(shù)據(jù)
    /// </summary>
    public static string RequestUrl(string url)
    {
      return RequestUrl(url, "POST");
    }
    #endregion

    #region 請求Url,不發(fā)送數(shù)據(jù)
    /// <summary>
    /// 請求Url,不發(fā)送數(shù)據(jù)
    /// </summary>
    public static string RequestUrl(string url, string method)
    {
      // 設(shè)置參數(shù)
      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");

      //發(fā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù)
      HttpWebResponse response = request.GetResponse() as HttpWebResponse;
      //直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求
      Stream responseStream = response.GetResponseStream();
      StreamReader sr = new StreamReader(responseStream, Encoding.Default);
      //返回結(jié)果網(wǎng)頁(html)代碼
      string content = sr.ReadToEnd();
      return content;
    }
    #endregion
  }

注意:需要在微信公眾平臺中設(shè)置授權(quán)回調(diào)域

如何實現(xiàn)MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId

以上是“如何實現(xiàn)MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞標(biāo)題:如何實現(xiàn)MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article32/eoepc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號靜態(tài)網(wǎng)站App開發(fā)企業(yè)網(wǎng)站制作網(wǎng)站策劃定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)