Ejemplo n.º 1
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.º 2
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));
        }