Exemple #1
0
 /// <summary>
 /// 获取微信用户基本信息(包括UnionID机制)
 /// </summary>
 /// <param name="accessToken"></param>
 /// <param name="openid"></param>
 /// <returns></returns>
 public static MPUserInfoResult GetWxUserInfo(string wxApiUrl, out string errmsg)
 {
     errmsg = "";
     try
     {
         IHttpForm        _httpForm = new HttpForm("HOT-MPHelper", 15000, true, 8);
         HttpFormResponse _response = _httpForm.Get(new HttpFormGetRequest()
         {
             Url = wxApiUrl
         });
         MPUserInfoResult result = JsonConvert.DeserializeObject <MPUserInfoResult>(_response.Response);
         return(result);
     }
     catch (Exception ex)
     {
         errmsg = ex.Message;
         LogHelper.Write("获取微信用户基本信息(GetWxUserInfo)异常 -->:" + ex.Message);
         return(null);
     }
 }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Code.Equals("authdeny", StringComparison.CurrentCultureIgnoreCase))
            {
                this.WriteLog("当前用户取消了授权。");
                return;
            }

            //合法的链接都会带上code和state
            if (this.Code == "" || this.State == "")
            {
                this.WriteLog("不合法的请求");
                return;
            }
            //分析state中的数据
            this.DoAnalyzeState();
            if (this.NonceStr == "" || this.CustomerId == 0)
            {
                this.WriteLog("state参数残缺");
                return;
            }

            _redirectUrl = UserService.Instance.GetOAuthUrl(this.NonceStr);

            //获取带openid的信息
            OAuthAccessTokenResult atResult;
            OAuthUserInfoPlus      oauthUserInfo = null;

            try
            {
                string errmsg = string.Empty;
                //获取openid
                atResult = OAuth.GetAccessToken(this.AppId(this.CustomerId), this.AppSecret(this.CustomerId), this.Code);
                if (atResult.errcode != ReturnCode.请求成功)
                {
                    this.WriteLog(string.Format("code:{0},msg:{1}", atResult.errcode, atResult.errmsg));
                    return;
                }
                #region 使用高级授权获取用户信息
                try
                {
                    // oauthUserInfo = OAuth.GetUserInfo(atResult.access_token, atResult.openid);
                    //新方法
                    string           wxApiUrl = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", atResult.access_token, atResult.openid);
                    MPUserInfoResult result   = MPHelper.GetWxUserInfo(wxApiUrl, out errmsg);
                    if (result != null)
                    {
                        this.SetUserInfo(this.CustomerId, new WeixinOAuthUserInfoModel()
                        {
                            City       = result.city,
                            Country    = result.country,
                            Headimgurl = result.headimgurl,
                            Nickname   = result.nickname,
                            Openid     = result.openid,
                            Privilege  = result.privilege,
                            Province   = result.province,
                            Sex        = result.sex,
                            UnionID    = result.unionid
                        });
                        oauthUserInfo = new OAuthUserInfoPlus()
                        {
                            city       = result.city,
                            country    = result.country,
                            headimgurl = result.headimgurl,
                            nickname   = result.nickname,
                            openid     = result.openid,
                            privilege  = result.privilege,
                            province   = result.province,
                            sex        = result.sex,
                            unionid    = result.unionid
                        };
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Write("OAuth.GetAccessToken:" + ex.Message);
                    this.WriteLog(ex.Message);
                    return;
                }


                try
                {
                    string url = "";
                    #region 1、openid处理带上URL
                    url = this.AppendUrlParameters(_redirectUrl, "openid", atResult.openid);
                    #endregion

                    #region 2、授权用户的详细信息JSON带上URL
                    if (this.ReturnUserInfo && oauthUserInfo != null)
                    {
                        string encodedUserInfo = HttpContext.Current.Server.UrlEncode(JsonConvert.SerializeObject(oauthUserInfo));
                        url = this.AppendUrlParameters(url, "retuinfo", encodedUserInfo);
                    }
                    if (TryReg)
                    {
                        WeixinOAuthUserInfoModel wouInfo = null;
                        if (oauthUserInfo != null)
                        {
                            wouInfo = GetUserInfo(this.CustomerId);
                        }
                        int retuid = UserService.Instance.GetTopOAuthedUserId(this.CustomerId, atResult.openid);
                        if (retuid == 0)//注册
                        {
                            /**
                             * 执行用户注册操作
                             */
                            retuid = UserService.Instance.RegisterUser(this.CustomerId, wouInfo);
                        }
                        url = this.AppendUrlParameters(url, "retuid", retuid.ToString());
                    }

                    this.StoreOpenId(this.CustomerId, atResult.openid);
                    this.SetOpenId(this.CustomerId, atResult.openid);


                    Response.Redirect(url, false);

                    #endregion
                }
                catch (Exception)
                {
                    throw;
                }



                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }