public QuickLogOnResponse LogOn(QuickLogOnRequest request)
        {
            var userName = request.UserName;
            var lowerEmail = request.Email == null ? "" : request.Email.ToLowerInvariant();

            return LogOn(request, () =>
            {
                var user = _membershipService.CreateUser(new CreateUserParams(userName, Guid.NewGuid().ToString(), lowerEmail, null, null, true)) as UserPart;
                return user;
            });
        }
        public QuickLogOnResponse LogOn(QuickLogOnRequest request, Func<UserPart> createUser)
        {
            var userName = request.UserName;
            var lowerEmail = request.Email == null ? "" : request.Email.ToLowerInvariant();

            return LogOn(request, createUser, () =>
            {
                UserPart user = null;
                //var user = _orchardServices.ContentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName).List().FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(userName))
                {
                    user = _orchardServices.ContentManager.Query<UserPart, UserPartRecord>().Where(u => u.UserName == userName).List().FirstOrDefault();
                }
                else if (!string.IsNullOrWhiteSpace(lowerEmail))
                {
                    user = _orchardServices.ContentManager.Query<UserPart, UserPartRecord>().Where(u => u.Email == lowerEmail).List().FirstOrDefault();
                }
                return user;
            });
        }
        public QuickLogOnResponse Auth(WorkContext wc, string code, string error, string returnUrl)
        {
            if (string.IsNullOrEmpty(code))
            {
                error = "无效的code";
            }
            else
            {
                var token = GetAccessToken(wc, code);
                if (!string.IsNullOrEmpty(token))
                {
                    var openId = GetOpenId(token);
                    if (!string.IsNullOrEmpty(openId))
                    {
                        var request = new QuickLogOnRequest
                        {
                            UserName = openId,
                            RememberMe = false,
                            ReturnUrl = returnUrl
                        };
                        var lowerEmail = request.Email == null ? "" : request.Email.ToLowerInvariant();
                        return _quickLogOnService.LogOn(request, () =>
                        {
                            var model = GetUserInfo(token, openId);

                            if (model.ret < 0)
                                throw new Exception(string.Format("[{0}]{1}", model.ret, model.msg));

                            UserPart user = _membershipService.CreateUser(new CreateUserParams(request.UserName, Guid.NewGuid().ToString(), lowerEmail, null, null, true)) as UserPart;

                            var record = new QQUserInfoPartRecord
                            {
                                UserId = user.Id,
                                nickname = model.nickname,
                                figureurl = model.figureurl,
                                figureurl_1 = model.figureurl_1,
                                figureurl_2 = model.figureurl_2,
                                figureurl_qq_1 = model.figureurl_qq_1,
                                figureurl_qq_2 = model.figureurl_qq_2,
                                gender = model.gender,
                                is_yellow_vip = model.is_yellow_vip,
                                vip = model.vip,
                                yellow_vip_level = model.yellow_vip_level,
                                level = model.level,
                                is_yellow_year_vip = model.is_yellow_year_vip,
                            };
                            _QQRepository.Create(record);

                            return user;
                        });
                    }
                    error = "无效的OpenID";
                }
                else
                {
                    error = "无效的访问令牌";
                }
            }
            return new QuickLogOnResponse { Error = T("QQ登录失败: {0}", error), ReturnUrl = returnUrl };
        }
        public QuickLogOnResponse Auth(WorkContext wc, string code, string returnUrl)
        {
            string error = null;
            if (string.IsNullOrEmpty(code))
            {
                error = "无效的code";
            }
            else
            {
                var token = GetAccessToken(wc, code);
                if (!string.IsNullOrEmpty(token.access_token))
                {
                    var request = new QuickLogOnRequest
                    {
                        UserName = token.openid,
                        RememberMe = false,
                        ReturnUrl = returnUrl
                    };
                    var lowerEmail = request.Email == null ? "" : request.Email.ToLowerInvariant();
                    return _quickLogOnService.LogOn(request, () =>
                    {
                        UserPart user = _membershipService.CreateUser(new CreateUserParams(request.UserName, Guid.NewGuid().ToString(), lowerEmail, null, null, true)) as UserPart;

                        var model = GetUserInfo(token.access_token, token.openid);

                        var record = new WinXinUserInfoPartRecord
                        {
                            UserId = user.Id,
                            openid = model.openid,
                            nickname = model.nickname,
                            sex = model.sex,
                            province = model.province,
                            city = model.city,
                            country = model.country,
                            headimgurl = model.headimgurl,
                            privilege = string.Join(",", model.privilege),

                        };
                        _winXinUserInfoPartRecordRepository.Create(record);

                        return user;
                    });
                }
                else
                {
                    error = string.Format("[{0}]{1}", token.errcode, token.errmsg);
                }
            }
            return new QuickLogOnResponse { Error = T("微信登录失败: {0}", error), ReturnUrl = returnUrl };
        }
        public QuickLogOnResponse LogOn(QuickLogOnRequest request, Func<UserPart> createUser, Func<UserPart> findUser)
        {
            var currentUser = _authenticationService.GetAuthenticatedUser();
            if (currentUser != null) _authenticationService.SignOut();

            UserPart user = findUser();

            if (user == null)
            {
                try
                {
                    user = createUser();
                    if (user == null)
                    {
                        return new QuickLogOnResponse { User = null, Error = T("用户无法被指定的快速登录凭据创建!") };
                    }
                }
                catch (Exception ex)
                {
                    return new QuickLogOnResponse { User = null, Error = T("{0}", ex.Message) };
                }
            }

            if (user.RegistrationStatus != UserStatus.Approved)
            {
                return new QuickLogOnResponse { User = null, Error = T("用户已被管理员禁用!"), ReturnUrl = request.ReturnUrl };
            }

            _authenticationService.SignIn(user, request.RememberMe);

            return new QuickLogOnResponse { User = user, ReturnUrl = request.ReturnUrl };
        }