Example #1
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         string code = HttpContext.Current.Request.QueryString["code"];
         string appid = ConfigurationManager.AppSettings["AppID"];
         if (string.IsNullOrEmpty(code))
         {
             log.WriteLog(string.Format("正在获取code"));
             Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fweixin.luxlead.com%2fWeb%2fAHmarried.aspx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ", appid));
             return;
         }
         else
         {
             OAuth_Token ot = new OAuth_Token();
             OAuth_User userinfo = new OAuth_User();
             string json = weixin.GetUserInfo(new string[] { code }, "GetAccessToken");
             ot = JsonHelper.ParseFromJson<OAuth_Token>(json);
             if (string.IsNullOrEmpty(ot.access_token))
             {
                 log.WriteLog(string.Format("正在获取access_token"));
                 Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fweixin.luxlead.com%2fWeb%2fAHmarried.aspx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ", appid));
                 return;
             }
             else
             {
                 try
                 {
                     json = weixin.GetUserInfo(new string[] { ot.access_token, ot.openid }, "GetUserInfo");
                     userinfo = JsonHelper.ParseFromJson<OAuth_User>(json);
                     foreach (Control control in Page.Controls)
                     {
                         if (control is Label)
                         {
                             Label txt = (Label)control;
                             txt.Text = userinfo.nickname;
                         }
                         if (control is HtmlImage)
                         {
                             HtmlImage imag = (HtmlImage)control;
                             imag.Src = userinfo.headimgurl;
                         }
                         if (control is HtmlGenericControl)
                         {
                             HtmlGenericControl lable = (HtmlGenericControl)control;
                             lable.InnerText = userinfo.nickname;
                         }
                     }
                     log.WriteLog(string.Format("获取到用户信息成功:{0}", json));
                 }
                 catch (Exception ex)
                 {
                     log.WriteLog(string.Format("获取到用户信息失败,错误信息:{0}", ex.Message));
                 }
             }
         }
     }
 }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取用户标识openId
            if (!IsPostBack)
            {
                string code = HttpContext.Current.Request.QueryString["code"];
                string appid = ConfigurationManager.AppSettings["AppID"];
                if (string.IsNullOrEmpty(code))
                {
                    Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fweixin.luxlead.com%2fWeb%2fRedPackage.aspx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ", appid));
                    return;
                }
                else
                {
                    OAuth_Token ot = new OAuth_Token();
                    string json = weixin.GetUserInfo(new string[] { code }, "GetAccessToken");
                    ot = JsonHelper.ParseFromJson<OAuth_Token>(json);

                    if (string.IsNullOrEmpty(ot.access_token))
                    {
                        log.WriteLog("获取到用户标识openId失败,无法获取到access_token,正在重定向!");
                        Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fweixin.luxlead.com%2fWeb%2fRedPackage.aspx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect ", appid));
                        return;
                    }
                    else
                    {
                        ViewState["isSucess"] = true;
                        ViewState["re_openid"] = ot.openid;
                        log.WriteLog(string.Format("获取到用户标识成功:openId:{0},access_token:{1}", ot.openid,ot.access_token));
                    }
                }
            }
        }