Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public ActionResult ThirdRegister()
        {
            pageResourceManager.InsertTitlePart("首次登录完善帐号资料");
            ThirdUser thirdUser = TempData.Get<ThirdUser>("thirdCurrentUser", null);
            TempData["thirdCurrentUser"] = thirdUser;
            ViewData["thirdCurrentUser"] = thirdUser;

            if (thirdUser == null)
                return Redirect(SiteUrls.Instance().Login());
            int expires_in = TempData.Get<int>("expires_in", 0);
            TempData["expires_in"] = expires_in;
            ViewData["expires_in"] = expires_in;

            if (new AccountBindingService().GetUserId(thirdUser.AccountTypeKey, thirdUser.Identification) > 0)
                return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel
                {
                    Title = "重复绑定",
                    Body = "已经有网站帐号绑定过,不允许重复绑定第三方帐号",
                    StatusMessageType = StatusMessageType.Hint
                }));

            ThirdAccountGetter thirdAccountGetter = ThirdAccountGetterFactory.GetThirdAccountGetter(thirdUser.AccountTypeKey);
            ViewData["AccountType"] = new AccountBindingService().GetAccountType(thirdUser.AccountTypeKey);
            ViewData["AccountTypeName"] = thirdAccountGetter.AccountTypeName;
            thirdUser.NickName = thirdUser.NickName.Replace("%", "").Replace("+", "").Replace(" ", "").Replace("/", "").Replace("?", "").Replace("&", "").Replace("=", "").Replace("#", "");
            var model = new ThirdRegisterEditModel();
            model.UserName = thirdUser.NickName;
            model.ShareToFirend = true;
            model.FollowOfficial = true;

            ViewData["Content"] = string.Format(ResourceAccessor.GetString("AccountBinding_ShareToFirend"), thirdAccountGetter.AccountTypeName, DIContainer.Resolve<ISettingsManager<SiteSettings>>().Get().SiteName, SiteUrls.FullUrl(SiteUrls.Instance().SiteHome()));

            return View(model);
        }