C#网页授权获取微信Openid

2021/4/26 20:28:56

本文主要是介绍C#网页授权获取微信Openid,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


string openid = ""; 
string appid = "微信公众号appid";
string appsecret = "秘钥";
if (Session["openid"] == null)
{
  string code = Request.QueryString["code"];
  string state = Request.QueryString["state"];
  if (code != null && code.Length > 0)
  {
    #region 获取到CODE
    string posturl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code
    + "&grant_type=authorization_code";
    string getstr =HttpGet(posturl, "utf-8");

    JsonData jd = JsonMapper.ToObject(getstr);
    openid = jd["openid"].ToString();
    Session["openid"] = openid;

    #endregion
  }
  else
  {
    string callbackurl = Request.Url.ToString();
    openid = (string)Session["openid"];
    if (openid == null || openid.Length <= 0)
    {
      string ecode = MD5Encode(callbackurl).ToLower().Substring(0, 15);
      Session["wx_encode"] = ecode;
      string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri="
      + Server.UrlEncode(callbackurl) + "&response_type=code&scope=snsapi_userinfo&state=" + ecode + "#wechat_redirect";
      Response.Redirect(url);
    }

  }

}else
{
  openid = (string)Session["openid"];
}

 



这篇关于C#网页授权获取微信Openid的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程