Exemplo n.º 1
0
        /// <summary>
        /// 跳过第三方注册
        /// </summary>
        /// <returns></returns>
        public ActionResult JumpThirdRegister()
        {
            ThirdUser thirdUser = TempData.Get<ThirdUser>("thirdCurrentUser", null);
            TempData["thirdCurrentUser"] = thirdUser;
            if (thirdUser != null && new AccountBindingService().GetUserId(thirdUser.AccountTypeKey, thirdUser.Identification) > 0)
                return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel
                {
                    Title = "重复绑定",
                    Body = "已经有网站帐号绑定过,不允许重复绑定第三方帐号",
                    StatusMessageType = StatusMessageType.Hint
                }));

            //创建用户,并且将对应用的用户与第三方账户绑定。
            UserCreateStatus userCreateStatus = UserCreateStatus.UnknownFailure;
            User user = Spacebuilder.Common.User.New();

            //生成用户名
            user.UserId = IdGenerator.Next();
            string userName = thirdUser.NickName;
            string thirUserName = "******" + userName;
            if (UserIdToUserNameDictionary.GetUserId(userName) <= 0)
                user.UserName = userName;
            else if (UserIdToUserNameDictionary.GetUserId(userName) <= 0)
                user.UserName = thirUserName;
            else
                user.UserName = user.UserId.ToString();

            //随机密码
            string password = string.Empty;
            string pwdchars = "abcdefghijklmnopqrstuvwxyz0123456789";
            int iRandNum;
            Random rnd = new Random();
            for (int i = 0; i < 6; i++)
            {
                iRandNum = rnd.Next(pwdchars.Length);
                password += pwdchars[iRandNum];
            }
            user.Password = password;

            // 和用户名一样
            user.NickName = user.UserName;
            user.AccountEmail = string.Empty;
            user.IsActivated = userSettings.AccountActivation == AccountActivation.Automatic;

            //创建用户
            membershipService.CreateUser(user, password, out userCreateStatus);
            if (userCreateStatus != UserCreateStatus.Created)
            {
                return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel
                {
                    Title = "创建网站账号失败",
                    Body = "创建网站账号失败,请联系管理员",
                    StatusMessageType = StatusMessageType.Hint
                }));
            }

            //创建用户资料
            UserProfileService userProfileService = new UserProfileService();
            UserProfile userProfile = UserProfile.New();
            userProfile.UserId = user.UserId;
            userProfile.Gender = thirdUser.Gender;
            userProfileService.Create(userProfile);

            //让用户登录
            authenticationService.SignIn(user, false);

            //绑定帐号
            if (!string.IsNullOrEmpty(thirdUser.AccountTypeKey))
            {
                AccountBinding account = new AccountBinding();
                account.AccountTypeKey = thirdUser.AccountTypeKey;
                account.Identification = thirdUser.Identification;
                account.UserId = user.UserId;
                account.AccessToken = thirdUser.AccessToken;
                int expires_in = TempData.Get<int>("expires_in", 0);
                if (expires_in > 0)
                    account.ExpiredDate = DateTime.UtcNow.AddSeconds(expires_in);
                new AccountBindingService().CreateAccountBinding(account);
            }

            if (Request.Cookies != null)
            {
                if (Request.Cookies.Get("returnUrl") != null && !string.IsNullOrEmpty(Request.Cookies.Get("returnUrl").Value))
                {
                    string returnUrl = Request.Cookies.Get("returnUrl").Value;
                    Response.Cookies["returnUrl"].Expires = DateTime.Now;
                    return Redirect(Tunynet.Utilities.WebUtility.UrlDecode(returnUrl));
                }

            }

            //根据站点配置判断应该跳转到什么页面
            if (userSettings.MyHomePageAsSiteEntry)
                return Redirect(SiteUrls.Instance().MyHome(user.UserId));
            return Redirect(SiteUrls.Instance().SiteHome());
        }
Exemplo n.º 2
0
        public ActionResult Register(RegisterEditModel model)
        {
            InviteFriendSettings inviteFriendSettings = inviteFriendSettingsManager.Get();
            SystemMessageViewModel message;
            bool isContinue = InviteRegister(out message);
            if (!isContinue)
            {
                return Redirect(SiteUrls.Instance().SystemMessage(TempData, message));
            }
            if (!ModelState.IsValid)
                return View(model);
            User user = model.AsUser();

            //根据站点配置设置用户属性
            user.IsActivated = userSettings.AccountActivation == AccountActivation.Automatic;

            //尝试创建用户
            IMembershipService membershipService = DIContainer.Resolve<IMembershipService>();
            UserCreateStatus userCreateStatus = UserCreateStatus.UnknownFailure;
            membershipService.CreateUser(user, model.Password, out userCreateStatus);

            if (userCreateStatus != UserCreateStatus.Created)
            {
                switch (userCreateStatus)
                {
                    case UserCreateStatus.DisallowedUsername:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "不允许的用户名");
                        break;
                    case UserCreateStatus.DuplicateEmailAddress:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的邮箱");
                        break;
                    case UserCreateStatus.DuplicateMobile:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的手机号");
                        break;
                    case UserCreateStatus.DuplicateUsername:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的用户名");
                        break;
                    case UserCreateStatus.InvalidPassword:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "不合法的密码");
                        break;
                    case UserCreateStatus.InvalidQuestionAnswer:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "不合法的问题答案");
                        break;
                    default:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "发生未知错误");
                        break;
                }
                model.Password = string.Empty;
                model.ConfirmPassword = string.Empty;
                return View(model);
            }

            UserProfileService userProfileService = new UserProfileService();
            UserProfile userProfile = UserProfile.New();
            userProfile.UserId = user.UserId;
            userProfileService.Create(userProfile);

            if (userSettings.EnableNotActivatedUsersToLogin || user.IsActivated)
                authenticationService.SignIn(user, false);

            if (Request.Cookies["invite"] != null)
            {
                string invite = Request.Cookies["invite"].Value;
                InvitationCode invitationCode = inviteFriendService.GetInvitationCodeEntity(invite);
                if (invitationCode != null)
                {
                    string errorMessage;
                    InviteFriendRecord inviteFriendRecord = new InviteFriendRecord
                    {
                        Code = invitationCode.Code,
                        DateCreated = DateTime.UtcNow,
                        InvitedUserId = user.UserId,
                        UserId = invitationCode.UserId
                    };
                    inviteFriendService.CreateInviteFriendRecord(inviteFriendRecord);
                    if (!invitationCode.IsMultiple)
                    {
                        bool isSuccess = MutualFollow(invitationCode, userService.GetFullUser(user.UserName), out errorMessage);
                        Response.Cookies.Remove("invite");
                        if (!isSuccess)
                        {
                            return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel
                            {
                                Title = "关注失败",
                                Body = errorMessage,
                                StatusMessageType = StatusMessageType.Hint
                            }));
                        }
                    }
                    else
                    {
                        User userFromDB = userService.GetFullUser(user.UserName);
                        if (userFromDB != null)
                        {
                            string token = Utility.EncryptTokenForInviteFriend(0.01, userFromDB.UserId);
                            return Redirect(SiteUrls.Instance().ConfirmFollow(token, Request.Cookies["invite"].Value));
                        }
                    }
                }

                //IUser userFromDB = userService.GetFullUser(model.UserName);
                //string token = null;
                //if (!userSettings.EnableNotActivatedUsersToLogin)
                //{
                //    token = Utility.EncryptTokenForInviteFriend(0.01, userFromDB.UserId);
                //    return Redirect(SiteUrls.Instance().ConfirmFollow(token));
                //}
                //User userFormDB = userService.GetFullUser(model.UserName);
                //string errorMessage;
                //AcceptInvitation(Request.Cookies["invite"].Value, userFormDB, out errorMessage);
            }

            return RegisterJumpByconfig(user);
        }
Exemplo n.º 3
0
        public ActionResult ThirdRegister(ThirdRegisterEditModel model)
        {
            ThirdUser thirdUser = TempData.Get<ThirdUser>("thirdCurrentUser", null);
            TempData["thirdCurrentUser"] = thirdUser;
            if (!ModelState.IsValid)
                return View(model.CleanUp());
            if (thirdUser != null && new AccountBindingService().GetUserId(thirdUser.AccountTypeKey, thirdUser.Identification) > 0)
                return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel
                {
                    Title = "重复绑定",
                    Body = "已经有网站帐号绑定过,不允许重复绑定第三方帐号",
                    StatusMessageType = StatusMessageType.Hint
                }));
            //数据通过验证,创建用户,并且将对应用的用户与第三方账户绑定。

            UserCreateStatus userCreateStatus = UserCreateStatus.UnknownFailure;
            User user = model.AsUser();

            user.IsActivated = userSettings.AccountActivation == AccountActivation.Automatic;

            membershipService.CreateUser(user, model.PassWord, out userCreateStatus);
            //如果没有创建成功
            if (userCreateStatus != UserCreateStatus.Created)
            {
                switch (userCreateStatus)
                {
                    case UserCreateStatus.DisallowedUsername:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的用户名");
                        break;
                    case UserCreateStatus.DuplicateEmailAddress:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的邮箱");
                        break;
                    case UserCreateStatus.DuplicateMobile:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的手机号");
                        break;
                    case UserCreateStatus.DuplicateUsername:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "重复的用户名");
                        break;
                    case UserCreateStatus.InvalidPassword:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "不合法的密码");
                        break;
                    case UserCreateStatus.InvalidQuestionAnswer:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "不合法的问题答案");
                        break;
                    default:
                        ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "发生未知错误");
                        break;
                }
                return View(model.CleanUp());
            }

            // 处理头像
            //if (!string.IsNullOrEmpty(thirdUser.UserAvatarUrl))
            //{
            //    try
            //    {
            //        WebRequest webRequest = WebRequest.Create(thirdUser.UserAvatarUrl);
            //        HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
            //        Stream stream = httpWebResponse.GetResponseStream();

            //        bool isImage = httpWebResponse.ContentType.IndexOf("image") > -1;
            //        if (isImage && stream != null && stream.CanRead)
            //        {
            //            MemoryStream memoryStream = new MemoryStream();
            //            const int bufferLength = 1024;
            //            int actual;
            //            byte[] buffer = new byte[bufferLength];

            //            while ((actual = stream.Read(buffer, 0, bufferLength)) > 0)
            //            {
            //                memoryStream.Write(buffer, 0, actual);
            //            }

            //            userService.UploadOriginalAvatar(user.UserId, memoryStream);
            //            userService.CropAvatar(user.UserId, 100, 100, 0, 0);
            //        }
            //    }
            //    catch (Exception e)
            //    {
            //        ILogger logger = LoggerFactory.GetLogger();
            //        logger.Error(e, "第三方帐号登陆时,下载用户头像报错");
            //    }
            //}

            UserProfileService userProfileService = new UserProfileService();
            UserProfile userProfile = UserProfile.New();
            userProfile.UserId = user.UserId;
            userProfile.Gender = thirdUser.Gender;
            userProfileService.Create(userProfile);

            if (userSettings.EnableNotActivatedUsersToLogin || user.IsActivated)
                authenticationService.SignIn(user, false);

            if (!string.IsNullOrEmpty(thirdUser.AccountTypeKey))
            {
                //绑定帐号
                AccountBinding account = new AccountBinding();
                account.AccountTypeKey = thirdUser.AccountTypeKey;
                account.Identification = thirdUser.Identification;
                account.UserId = user.UserId;
                account.AccessToken = thirdUser.AccessToken;
                int expires_in = TempData.Get<int>("expires_in", 0);
                if (expires_in > 0)
                    account.ExpiredDate = DateTime.UtcNow.AddSeconds(expires_in);
                new AccountBindingService().CreateAccountBinding(account);

                var thirdAccountGetter = ThirdAccountGetterFactory.GetThirdAccountGetter(thirdUser.AccountTypeKey);
                if (model.FollowOfficial)
                {
                    //自动关注官方帐号
                    thirdAccountGetter.FollowOfficialMicroBlog(thirdUser.AccessToken, thirdUser.Identification);
                }
                if (model.ShareToFirend)
                {
                    //自动推送一条微博
                    //string siteName = DIContainer.Resolve<ISiteSettingsManager>().Get().SiteName;
                    //string content = string.Format(ResourceAccessor.GetString("AccountBinding_ShareToFirend"), thirdAccountGetter.AccountTypeName, siteName, SiteUrls.FullUrl(SiteUrls.Instance().SiteHome()));
                    string content = Request.Form.Get<string>("Content", string.Empty);
                    if (!string.IsNullOrEmpty(content))
                        thirdAccountGetter.CreateMicroBlog(thirdUser.AccessToken, content, thirdUser.Identification);
                }
            }

            if (Request.Cookies["invite"] != null)
            {
                string invite = Request.Cookies["invite"].Value;
                InvitationCode invitationCode = inviteFriendService.GetInvitationCodeEntity(invite);
                if (invitationCode != null)
                {
                    InviteFriendRecord inviteFriendRecord = new InviteFriendRecord
                    {
                        Code = invitationCode.Code,
                        DateCreated = DateTime.UtcNow,
                        InvitedUserId = user.UserId,
                        UserId = invitationCode.UserId
                    };
                    inviteFriendService.CreateInviteFriendRecord(inviteFriendRecord);
                    if (!invitationCode.IsMultiple)
                    {
                        Response.Cookies.Remove("invite");
                        string errorMessage;
                        bool isSuccess = MutualFollow(invitationCode, userService.GetFullUser(user.UserName), out errorMessage);
                        if (!isSuccess)
                        {
                            return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel
                            {
                                Title = "关注失败",
                                Body = errorMessage,
                                StatusMessageType = StatusMessageType.Hint
                            }));
                        }
                    }
                    else
                    {
                        User userFromDB = userService.GetFullUser(user.UserName);
                        if (userFromDB != null)
                        {
                            string token = Utility.EncryptTokenForInviteFriend(0.01, userFromDB.UserId);
                            return Redirect(SiteUrls.Instance().ConfirmFollow(token, Request.Cookies["invite"].Value));
                        }
                    }
                }
            }

            return RegisterJumpByconfig(user);
        }