Ejemplo n.º 1
0
        /// <summary>
        /// 登陆
        /// </summary>
        public ActionResult Login()
        {
            //返回url
            string returnUrl = WebHelper.GetQueryString("returnUrl");

            if (returnUrl.Length == 0)
            {
                returnUrl = "/";
            }

            if (WorkContext.ShopConfig.LoginType == "")
            {
                return(PromptView(returnUrl, "商城目前已经关闭登陆功能!"));
            }
            if (WorkContext.Uid > 0)
            {
                return(PromptView(returnUrl, "您已经登录,无须重复登录!"));
            }


            string returnurl = string.Format(@"http://{0}{1}", BSPConfig.ShopConfig.SiteUrl, Url.Action("CallBack"));
            string url       = WeiXinOAuth.GetWeiXinRqUrl(pluginSetInfo.AppID, pluginSetInfo.AppSecret, returnurl, "", "", "", "code");

            //WebHelper.GetRequestData(url, "");
            return(Redirect(url));
        }
Ejemplo n.º 2
0
        private void DoCheck()
        {
            CheckAgent();
            if (HttpContext.Current.Session["user"] != null)
            {
                WeiXinUserInfo userinfo = HttpContext.Current.Session["user"] as WeiXinUserInfo;
                Nickname   = userinfo.nickname;
                Headimgurl = userinfo.headimgurl;
                Openid     = userinfo.openid;
            }
            else
            {
                //获取appId,appSecret的配置信息
                string          appId       = System.Configuration.ConfigurationSettings.AppSettings["appid"];
                string          appSecret   = System.Configuration.ConfigurationSettings.AppSettings["secret"];
                BLL.WeiXinOAuth weixinOAuth = new WeiXinOAuth();
                //微信第一次握手后得到的code 和state
                _code = HttpContext.Current.Request.QueryString["code"] == null ? "" : HttpContext.Current.Request.QueryString["code"].ToString();

                if (_code == "" || _code == "authdeny")
                {
                    if (_code == "")
                    {
                        //发起授权(第一次微信握手)
                        string _authUrl = weixinOAuth.GetWeiXinCode(appId, appSecret, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.ToString()));
                        HttpContext.Current.Response.Redirect(_authUrl, true);
                    }
                    else
                    { // 用户取消授权
                        GoNoFound("必须要您的授权才能进入哦!");
                    }
                }
                else
                {
                    //获取微信的Access_Token(第二次微信握手)
                    Model.WeiXinAccessTokenResult modelResult = weixinOAuth.GetWeiXinAccessToken(appId, appSecret, _code);

                    //获取微信的用户信息(第三次微信握手)
                    Model.WeiXinUserInfoResult _userInfo = weixinOAuth.GetWeiXinUserInfo(modelResult.SuccessResult.access_token, modelResult.SuccessResult.openid);

                    //用户信息(判断是否已经获取到用户的微信用户信息)
                    if (_userInfo.Result && _userInfo.UserInfo.openid != "")
                    {
                        WeiXinUserInfo UserInfo = new WeiXinUserInfo();
                        //保存获取到的用户微信用户信息,并保存到数据库中
                        Nickname            = _userInfo.UserInfo.nickname;
                        Headimgurl          = _userInfo.UserInfo.headimgurl;
                        Openid              = _userInfo.UserInfo.openid;
                        UserInfo.openid     = _userInfo.UserInfo.openid;
                        UserInfo.headimgurl = _userInfo.UserInfo.headimgurl;
                        UserInfo.nickname   = _userInfo.UserInfo.nickname;
                        HttpContext.Current.Session["user"] = UserInfo;
                    }
                    else
                    {
                        GoNoFound("获取用户OpenId失败");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public ActionResult WeiXinLogin()
        {
            //获取appId,appSecret的配置信息
            string appId       = ConfigurationManager.AppSettings["weixin:appid"];
            string appSecret   = ConfigurationManager.AppSettings["weixin:secret"];
            var    weixinOAuth = new WeiXinOAuth();
            //微信第一次握手后得到的code 和state
            string _code  = Request["code"];
            string _state = Request["state"];

            if (string.IsNullOrEmpty(_code) || _code == "authdeny")
            {
                if (string.IsNullOrEmpty(_code))
                {
                    //发起授权(第一次微信握手)
                    string _authUrl = weixinOAuth.GetWeiXinCode(appId, appSecret, Server.UrlEncode(Request.Url.ToString()));
                    Response.Redirect(_authUrl, true);
                }
                else
                {   // 用户取消授权
                    Response.Redirect("~/Error.html", true);
                }
            }
            else
            {
                //获取微信的Access_Token(第二次微信握手)
                var modelResult = weixinOAuth.GetWeiXinAccessToken(appId, appSecret, _code);
                //获取微信的用户信息(第三次微信握手)
                var _userInfo = weixinOAuth.GetWeiXinUserInfo(modelResult.SuccessResult.access_token, modelResult.SuccessResult.openid);
                //用户信息(判断是否已经获取到用户的微信用户信息)
                if (_userInfo.Result && _userInfo.UserInfo.openid != "")
                {
                    //如果存在则自动登录
                    var user = _userService.GetUserByOpenId(_userInfo.UserInfo.openid);
                    if (user != null)
                    {
                        AutoLogin(user);
                    }

                    Session["openid"] = _userInfo.UserInfo.openid;

                    return(new RedirectResult(Session["ReturnUrl"] == null ? "/exchange/" : Session["ReturnUrl"].ToString()));
                }
                else
                {
                    throw new Exception("获取用户OpenId失败");
                }
            }

            return(Json(new { Success = true }, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 回调
        /// </summary>
        public ActionResult CallBack()
        {
            //返回url
            string returnUrl = WebHelper.GetQueryString("returnUrl");

            if (returnUrl.Length == 0)
            {
                returnUrl = "/";
            }
            string[] rtval = Request.QueryString.AllKeys;
            if (Array.IndexOf <string>(rtval, "code") == -1)
            {
                return(PartialView("您未授权微信登录,请重新扫描登陆"));
            }

            if (WorkContext.ShopConfig.LoginType == "")
            {
                return(PromptView(returnUrl, "目前已经关闭登陆功能!"));
            }
            if (WorkContext.Uid > 0)
            {
                return(PromptView(returnUrl, "您已经登录,无须重复登录!"));
            }
            //返回的随机值
            string backSalt = WebHelper.GetQueryString("state");
            //Authorization Code
            string code = WebHelper.GetQueryString("code");

            //获取access_token
            string tokenurl = WeiXinOAuth.GetWeiXinRqUrl(pluginSetInfo.AppID, pluginSetInfo.AppSecret, "", code, "", "", "access_token");
            WeiXinAccessTokenResult token = WeiXinOAuth.GetAccessToken(tokenurl, pluginSetInfo.AppID, pluginSetInfo.AppSecret);

            if (token.ErrorResult.errcode == 40029)
            {
                return(PartialView("获取微信授权码错误,请重新扫描登陆"));
            }


            //判断此用户是否已经存在
            int uid = OAuths.GetUidByOpenIdAndServer(token.SuccessResult.openid, pluginSetInfo.Server);

            if (uid > 0)//存在时
            {
                PartUserInfo partUserInfo = Users.GetPartUserById(uid);
                //更新用户最后访问
                Users.UpdateUserLastVisit(partUserInfo.Uid, DateTime.Now, WorkContext.IP, WorkContext.RegionId);

                ShopUtils.SetUserCookie(partUserInfo, -1, "web");

                return(Redirect("/"));
            }
            else
            {
                //获取用户信息
                string userurl = WeiXinOAuth.GetWeiXinRqUrl("", "", "", "", token.SuccessResult.openid, token.SuccessResult.access_token, "openid");
                WeiXinUserInfoResult userinfo = WeiXinOAuth.GetWeiXinUserInfo(userurl);
                if (userinfo.ErrorMsg.errcode == 40003)
                {
                    return(PartialView("获取用户信息失败,请重新扫描登陆"));
                }


                UserInfo userInfo = OAuths.CreateOAuthUser(userinfo.UserInfo.nickname, pluginSetInfo.UNamePrefix, token.SuccessResult.openid,
                                                           pluginSetInfo.Server, WorkContext.RegionId, userinfo.UserInfo.unionid);
                if (userInfo != null)
                {
                    ShopUtils.SetUserCookie(userInfo, -1, "web");
                    return(Redirect("/"));
                }
                else
                {
                    return(PartialView("用户创建失败"));
                }
            }
        }