亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId

 更新時間:2016年09月14日 15:03:53   作者:赤月奇  
這篇文章主要為大家詳細介紹了MVC微信網(wǎng)頁授權(quán),在模板頁中獲取用戶openid,感興趣的小伙伴們可以參考一下

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

注意框架為MVC 開發(fā)微信公眾平臺。場景為,在模板頁中獲取用戶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字符串某節(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ù)
      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ā)送請求并獲取相應回應數(shù)據(jù)
      HttpWebResponse response = request.GetResponse() as HttpWebResponse;
      //直到request.GetResponse()程序才開始向目標網(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
  } 

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • ASP.NET Core MVC學習之視圖組件(View Component)

    ASP.NET Core MVC學習之視圖組件(View Component)

    這篇文章主要給大家介紹了關于ASP.NET Core MVC學習之視圖組件(View Component)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core MVC具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-08-08
  • ASP.NET中用healthMonitor屬性用法

    ASP.NET中用healthMonitor屬性用法

    ASP.NET中用healthMonitor屬性用法...
    2006-09-09
  • MvcPager分頁控件使用注意事項

    MvcPager分頁控件使用注意事項

    這篇文章主要為大家詳細介紹了MvcPager分頁控件使用的注意事項,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • ASP.NET用戶控件如何使用

    ASP.NET用戶控件如何使用

    這篇文章主要介紹了ASP.NET用戶控件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-09-09
  • ASP.NET Core中使用默認MVC路由的配置

    ASP.NET Core中使用默認MVC路由的配置

    這篇文章主要介紹了ASP.NET Core中使用默認MVC路由的配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Json日期格式問題的四種解決方法(超詳細)

    Json日期格式問題的四種解決方法(超詳細)

    這篇文章主要介紹了Json日期格式問題的四種解決方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • asp.net 驗證碼生成和刷新及驗證

    asp.net 驗證碼生成和刷新及驗證

    在.NET中新建一個Web項目,添加一個.ASPX頁面,取名VerifyCode.aspx, 轉(zhuǎn)到其代碼編輯狀態(tài),將下面的代碼Copy&Paste過去就可以用了,有什么意見或問題歡迎提出^@^
    2009-10-10
  • 使用Hangfire+.NET?6實現(xiàn)定時任務管理(推薦)

    使用Hangfire+.NET?6實現(xiàn)定時任務管理(推薦)

    這篇文章主要介紹了使用Hangfire+.NET?6實現(xiàn)定時任務管理,通過引入Hangfire相關的Nuget包并對Hangfire進行服務配置,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • .NET通過字典給類賦值實現(xiàn)代碼

    .NET通過字典給類賦值實現(xiàn)代碼

    這篇文章主要介紹了.NET通過字典給類賦值實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • ASP.NET在線文本編輯控件的使用(第6節(jié))

    ASP.NET在線文本編輯控件的使用(第6節(jié))

    這篇文章主要介紹了ASP.NET在線文本編輯控件的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08

最新評論