示例#1
0
 protected void Button1_Click(object sender, EventArgs e)
 {
     var request = new SnsOAuthAccessTokenRequest
     {
         AppID = m_appIdent.AppID,
         AppSecret = m_appIdent.AppSecret,
         Code = Label1.Text
     };
     var response = m_client.Execute(request);
     if (!response.IsError)
     {
         Label2.Text = response.AccessToken;
         Label3.Text = response.RefreshToken;
         Label5.Text = response.OpenId;
     }
     else
     {
         Label6.Text = response.ToString();
     }
 }
示例#2
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            var request = new SnsOAuthAccessTokenRequest
            {
                AppID     = m_appIdent.AppID,
                AppSecret = m_appIdent.AppSecret,
                Code      = Label1.Text
            };
            var response = m_client.Execute(request);

            if (!response.IsError)
            {
                Label2.Text = response.AccessToken;
                Label3.Text = response.RefreshToken;
                Label5.Text = response.OpenId;
            }
            else
            {
                Label6.Text = response.ToString();
            }
        }
示例#3
0
        public void ProcessRequest(HttpContext context)
        {
            var accesstoken = CookiesHelper.GetCookie(accesstokenKey);
            SnsOAuthAccessTokenResponse AccessToken = null;
            var m_client   = new DefaultApiClient();
            var m_appIdent = new AppIdentication(wxappid, wxappsecret);

            string State = context.Request["state"];

            string jumpUrl = "index.html";

            try
            {
                if (accesstoken == null)
                {
                    string code = context.Request["Code"];



                    var request = new SnsOAuthAccessTokenRequest
                    {
                        AppID     = m_appIdent.AppID,
                        AppSecret = m_appIdent.AppSecret,
                        Code      = code
                    };

                    AccessToken = m_client.Execute(request);

                    if (AccessToken.IsError)
                    {
                        throw new Exception("获取网页授权accesstoken失败。" +
                                            JsonHelper.ReBuilder(request) + "\r\n" +
                                            AccessToken.ErrorMessage);
                    }

                    CookiesHelper.AddCookie("webpage_accesstoken",
                                            JsonHelper.ReBuilder(AccessToken),
                                            DateTime.Now.AddSeconds(AccessToken.ExpiresIn - 600));
                }
                else
                {
                    AccessToken = JsonHelper.Build <SnsOAuthAccessTokenResponse>(accesstoken.Value);
                }

                string unionid = AccessToken.UnionId;

                if (String.IsNullOrEmpty(unionid))
                {
                    unionid = AccessToken.OpenId;
                }

                string openid = AccessToken.OpenId;
                var    query  = new RequestOperation <string>();

                query.Header = new HeaderInfo()
                {
                    DeviceID    = 5,
                    DisplayName = "customer",
                    UserID      = 1
                };

                query.Body = unionid;

                var service = new CustomerBP();

                var data = service.LoginByWechatAccount(query);
                //不存在此用户
                if (data.ErrCode == 1)
                {
                    var wexinInfo = new SnsUserInfoRequest
                    {
                        OAuthToken = AccessToken.AccessToken,
                        OpenId     = AccessToken.OpenId,
                        Lang       = Language.CN
                    };

                    var userinfo_res = m_client.Execute(wexinInfo);
                    if (userinfo_res.IsError)
                    {
                        throw new Exception("获取用户信息失败2。" +
                                            JsonHelper.ReBuilder(wexinInfo) + "\r\n" +
                                            JsonHelper.ReBuilder(userinfo_res));
                    }


                    #region 注册

                    string url          = HttpUtility.UrlDecode(State);
                    int    invateUserId = 0;
                    if (!string.IsNullOrEmpty(State))
                    {
                        url          = domian + unescape(url);
                        invateUserId = getInveteUser(url);
                    }

                    var register = new RequestOperation <RegisterData>();
                    register.Header             = query.Header;
                    register.Body               = new RegisterData();
                    register.Body.Account       = "";
                    register.Body.WechatAccount = unionid;
                    register.Body.QQAccount     = "";
                    register.Body.Face          = userinfo_res.HeadImageUrl;
                    register.Body.NickName      = register.Body.Name = userinfo_res.NickName;
                    register.Body.Password      = "******";

                    var register_res = service.Register(register);

                    if (register_res.ErrCode != 0)
                    {
                        throw new Exception("注册用户失败:" + register_res.Message);
                    }


                    LoginManage.SaveUserWeixinOpenId(userinfo_res.OpenId);
                    LoginManage.SaveUserInfo(register_res.Body.UserID);


                    if (!string.IsNullOrEmpty(State))
                    {
                        jumpUrl = replaceInveteUserParam(url);
                    }


                    #endregion
                }
                else
                {
                    //不准修改,此处用于微信支付!!!
                    LoginManage.SaveUserWeixinOpenId(AccessToken.OpenId);

                    LoginManage.SaveUserInfo(data.Body.UserID);

                    if (!string.IsNullOrEmpty(State))
                    {
                        string url = domian + HttpUtility.UrlDecode(State);
                        jumpUrl = replaceInveteUserParam(url);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteException("【微信网页授权】", ex, "");
            }

            context.Response.Redirect(jumpUrl);
        }