Beispiel #1
0
        public void LoginWX()
        {
            if (!string.IsNullOrEmpty(HttpContext.Request.Query["code"]))
            {
                //获取code码,以获取openid和access_token
                string code = HttpContext.Request.Query["code"];
                NLogHelper.InfoTxt($"LoginWX-Query:{HttpContext.Request.QueryString}");
                var accessToken = WXApi.GetOAuth2AccessTokenFromCode(code);
                if (!string.IsNullOrEmpty(accessToken.openid))
                {
                    string     url_userInfo = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken.access_token, accessToken.openid);
                    WXUserInfo wxUser       = HttpHelper.Get <WXUserInfo>(url_userInfo, false);

                    EUserInfo ui = TryInvitedUserComing(accessToken.openid, wxUser);
                    if (ui == null)
                    {
                        ui = _UserSrv.AddOrUpdateFromWXUser(wxUser);
                    }


                    WXLoginCallBack(ui);

                    string toPage = HttpContext.Request.Query["toPage"];
                    if (!string.IsNullOrEmpty(toPage))
                    {
                        if (toPage.Contains("/User/MyCourse") && ui.UserRole == UserRole.Teacher)
                        {
                            //  HttpContext.Response.Redirect("/Teacher/DayCourse");
                            return;
                        }
                        //微信QR支付页面
                        string amt = HttpContext.Request.Query["amt"];
                        if (!string.IsNullOrEmpty(amt))
                        {
                            var ct = HttpContext.Request.Query["ct"];
                            toPage += $"?amt={amt}&ct={ct}";
                        }
                        HttpContext.Response.Redirect(toPage);
                    }
                }
            }
            else
            {
                try
                {
                    var reUrl = $"https://edu.iqianba.cn/User/Login{Request.QueryString}";
                    //   NLogHelper.InfoTxt($"Login-reUrl:{reUrl}");
                    var       redirect_uri = System.Web.HttpUtility.UrlEncode(reUrl, System.Text.Encoding.UTF8);
                    WxPayData data         = new WxPayData();
                    data.SetValue("appid", WxConfig.APPID);
                    data.SetValue("redirect_uri", redirect_uri);
                    data.SetValue("response_type", "code");
                    data.SetValue("scope", "snsapi_userinfo");
                    data.SetValue("state", "1" + "#wechat_redirect");
                    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();

                    HttpContext.Response.Redirect(url);
                }
                catch (Exception ex)
                {
                    NLogHelper.ErrorTxt($"LoginWX:{ex.Message}");
                }

                return;
            }
        }