예제 #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginConfig loginConfig = new LoginConfig();

            this.Appid     = loginConfig.AppKey;
            this.Appsecret = loginConfig.AppSecret;
            this.code      = RequestHelper.GetQueryString <string>("code");
            if (!string.IsNullOrEmpty(code))
            {
                #region 获取access_token
                Access_tokenResult access_tokenResult = new Access_tokenResult();
                //如果session有值
                if (Session["expires_in"] != null && Session["access_token"] != null && Session["openid"] != null)
                {//如果Session["access_token"]没过期
                    if (Convert.ToInt32(Session["expires_in"]) >= ShopCommon.ConvertDateTimeInt(DateTime.Now))
                    {
                        access_tokenResult.access_token = Session["access_token"].ToString();
                        access_tokenResult.openid       = Session["openid"].ToString();
                    }
                    else//如果Session["access_token"]过期,根据refresh_token刷新access_token
                    {
                        if (Session["refresh_token"] != null)
                        {
                            access_tokenResult = RefreshAccess_token(Session["refresh_token"].ToString());
                        }
                    }
                }
                else
                {
                    //根据code获取access_token
                    access_tokenResult = CodeGetOpenidAndAccess_token(this.code);
                    if (!string.IsNullOrEmpty(access_tokenResult.errcode) || string.IsNullOrEmpty(access_tokenResult.access_token) || string.IsNullOrEmpty(access_tokenResult.openid))
                    {
                        Response.Write("参数错误,请稍后重试");
                        Response.End();
                    }
                    else
                    {
                        //验证access_token是否有效,失效了重新获取
                        WeChatMsg _msg = Check_Access_token(access_tokenResult.access_token, access_tokenResult.openid);
                        if (_msg.errcode != "0")
                        {
                            Response.Write("参数错误,请稍后重试");
                            Response.End();
                        }
                        else
                        {//access_token有效,重新赋值session
                            Session["expires_in"]    = ShopCommon.ConvertDateTimeInt(DateTime.Now) + 7000;
                            Session["access_token"]  = access_tokenResult.access_token;
                            Session["refresh_token"] = access_tokenResult.refresh_token;
                            Session["openid"]        = access_tokenResult.openid;
                        }
                    }
                }
                #endregion

                //获取userinfo
                Snsapi_userinfo snsapi_userinfo = GetUserinfo(access_tokenResult.access_token, access_tokenResult.openid);
                if (!string.IsNullOrEmpty(snsapi_userinfo.errcode) || string.IsNullOrEmpty(snsapi_userinfo.openid))
                {
                    Response.Write("参数错误,请稍后重试");
                    Response.End();
                }
                else
                {
                    string openID = snsapi_userinfo.openid;
                    //string openID = access_tokenResult.openid;
                    openID = "wx-" + openID;
                    //如果没有用户添加用户
                    int      userID   = UserBLL.Read(openID).Id;
                    UserInfo userInfo = new UserInfo();
                    if (userID <= 0)
                    {
                        userInfo.UserName     = Server.UrlDecode(snsapi_userinfo.nickname);
                        userInfo.UserPassword = StringHelper.Password(Guid.NewGuid().ToString(), (PasswordType)ShopConfig.ReadConfigInfo().PasswordType);
                        userInfo.Photo        = snsapi_userinfo.headimgurl;
                        int _sex = userInfo.Sex;
                        int.TryParse(snsapi_userinfo.sex, out _sex);
                        //微信sex:1 男,2 女, 0 未知
                        //本站sex:1 男,2 女, 3 未知
                        userInfo.Sex           = _sex == 0 ? 3 : _sex;
                        userInfo.Email         = "";
                        userInfo.RegisterIP    = ClientHelper.IP;
                        userInfo.RegisterDate  = RequestHelper.DateNow;
                        userInfo.LastLoginIP   = ClientHelper.IP;
                        userInfo.LastLoginDate = RequestHelper.DateNow;
                        userInfo.FindDate      = RequestHelper.DateNow;
                        userInfo.Status        = (int)UserStatus.Normal;
                        userInfo.OpenId        = openID;
                        userID = UserBLL.Add(userInfo);
                    }
                    //当前用户登录
                    userInfo = UserBLL.Read(userID);
                    UserBLL.UserLoginInit(userInfo);
                    userInfo.LastLoginIP   = ClientHelper.IP;
                    userInfo.LastLoginDate = RequestHelper.DateNow;
                    UserBLL.Update(userInfo);
                    //跳转至会员中心
                    Response.Write("<script> location.href='/Mobile/User/Index.html'</script>");
                    Response.End();
                    //ResponseHelper.Redirect("/User/Index.html");
                }
            }
        }
예제 #2
0
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            this.code = RequestHelper.GetQueryString <string>("code");
            if (!string.IsNullOrEmpty(code))
            {
                #region 获取access_token
                Access_tokenResult access_tokenResult = new Access_tokenResult();
                //如果session有值
                if (context.Session["expires_in"] != null && context.Session["access_token"] != null && context.Session["openid"] != null)
                {//如果Session["access_token"]没过期
                    if (Convert.ToInt32(context.Session["expires_in"]) >= ShopCommon.ConvertDateTimeInt(DateTime.Now))
                    {
                        access_tokenResult.access_token = context.Session["access_token"].ToString();
                        access_tokenResult.openid       = context.Session["openid"].ToString();
                    }
                    else//如果Session["access_token"]过期,根据refresh_token刷新access_token
                    {
                        if (context.Session["refresh_token"] != null)
                        {
                            access_tokenResult = RefreshAccess_token(context.Session["refresh_token"].ToString());
                        }
                    }
                }
                else
                {
                    //根据code获取access_token
                    access_tokenResult = CodeGetOpenidAndAccess_token(this.code);
                    if (!string.IsNullOrEmpty(access_tokenResult.errcode) || string.IsNullOrEmpty(access_tokenResult.access_token) || string.IsNullOrEmpty(access_tokenResult.openid))
                    {
                        context.Response.Write("参数错误,请稍后重试");
                        context.Response.End();
                    }
                    else
                    {
                        //验证access_token是否有效,失效了重新获取
                        WeChatMsg _msg = Check_Access_token(access_tokenResult.access_token, access_tokenResult.openid);
                        if (_msg.errcode != "0")
                        {
                            context.Response.Write("参数错误,请稍后重试");
                            context.Response.End();
                        }
                        else
                        {//access_token有效,重新赋值session
                            context.Session["expires_in"]    = ShopCommon.ConvertDateTimeInt(DateTime.Now) + 7000;
                            context.Session["access_token"]  = access_tokenResult.access_token;
                            context.Session["refresh_token"] = access_tokenResult.refresh_token;
                            context.Session["openid"]        = access_tokenResult.openid;
                        }
                    }
                }
                #endregion

                //获取userinfo
                Snsapi_userinfo snsapi_userinfo = GetUserinfo(access_tokenResult.access_token, access_tokenResult.openid);
                if (!string.IsNullOrEmpty(snsapi_userinfo.errcode) || string.IsNullOrEmpty(snsapi_userinfo.openid))
                {
                    context.Response.Write("参数错误,请稍后重试");
                    context.Response.End();
                }
                else
                {
                    string openID = snsapi_userinfo.openid;
                    //string openID = access_tokenResult.openid;
                    openID = "wx-" + openID;
                    //如果没有用户添加用户
                    int      userID   = UserBLL.Read(openID).Id;
                    UserInfo userInfo = new UserInfo();
                    if (userID <= 0)
                    {
                        userInfo.UserName     = snsapi_userinfo.nickname;
                        userInfo.UserPassword = StringHelper.Password(Guid.NewGuid().ToString(), (PasswordType)ShopConfig.ReadConfigInfo().PasswordType);
                        userInfo.Photo        = snsapi_userinfo.headimgurl;
                        int _sex = userInfo.Sex;
                        int.TryParse(snsapi_userinfo.sex, out _sex);
                        //微信sex:1 男,2 女, 0 未知
                        //本站sex:1 男,2 女, 3 未知
                        userInfo.Sex           = _sex == 0 ? 3 : _sex;
                        userInfo.Email         = "";
                        userInfo.RegisterIP    = ClientHelper.IP;
                        userInfo.RegisterDate  = RequestHelper.DateNow;
                        userInfo.LastLoginIP   = ClientHelper.IP;
                        userInfo.LastLoginDate = RequestHelper.DateNow;
                        userInfo.FindDate      = RequestHelper.DateNow;
                        userInfo.Status        = (int)UserStatus.Normal;
                        userInfo.OpenId        = openID;
                        userID = UserBLL.Add(userInfo);
                    }
                    //当前用户登录
                    userInfo = UserBLL.Read(userID);
                    UserBLL.UserLoginInit(userInfo);
                    userInfo.LastLoginIP   = ClientHelper.IP;
                    userInfo.LastLoginDate = RequestHelper.DateNow;
                    UserBLL.Update(userInfo);
                    //跳转至会员中心
                    ResponseHelper.Redirect("/mobile/User/Index.html");
                }
            }
        }