zoukankan      html  css  js  c++  java
  • 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"];
    }

     

  • 相关阅读:
    冲刺第七,八天(5月27,28日)
    作业4 阅读《构建之法》 第5.5 第6 第7章
    用户模拟+spec
    第二阶段
    第一次Spring总结
    小组互评和自评
    SPRINT四则运算(第二天)
    开始第一段SPRINT
    四则运算APP
    四则运算 测试与封装 (完善) 5.2 5.3
  • 原文地址:https://www.cnblogs.com/dsjbk/p/14705426.html
Copyright © 2011-2022 走看看