Example #1
0
        public ActionResult Settings(FormCollection collection)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var user = UserService.GetUser(User.Identity.Name);

                if (!string.IsNullOrWhiteSpace(collection["slug"]))
                {
                    user.Slug = collection["slug"];
                }
                if (!string.IsNullOrWhiteSpace(collection["website"]))
                {
                    user.Website = collection["website"];
                }
                if (!string.IsNullOrWhiteSpace(collection["Signature"]))
                {
                    user.Signature = collection["Signature"];
                }

                var metaList = new List<UserMeta>();

                var gender = new UserMeta();
                gender.MetaKey = AppConstants.UserGender;
                gender.MetaValue = collection[AppConstants.UserGender];
                metaList.Add(gender);

                var sexTrend = new UserMeta();
                sexTrend.MetaKey = AppConstants.UserSexTrend;
                sexTrend.MetaValue = collection[AppConstants.UserSexTrend];
                metaList.Add(sexTrend);

                var love = new UserMeta();
                love.MetaKey = AppConstants.UserLove;
                love.MetaValue = collection[AppConstants.UserLove];
                metaList.Add(love);

                var blood = new UserMeta();
                blood.MetaKey = AppConstants.UserBlood;
                blood.MetaValue = collection[AppConstants.UserBlood];
                metaList.Add(blood);

                var birthday = new UserMeta();
                birthday.MetaKey = AppConstants.UserBirthday;
                var birthdayValue = DateTime.UtcNow;
                DateTime.TryParse(collection[AppConstants.UserBirthday], out birthdayValue);
                birthday.MetaValue = birthdayValue.ToString();
                metaList.Add(birthday);

                var province = new UserMeta();
                province.MetaKey = AppConstants.UserProvince;
                province.MetaValue = collection[AppConstants.UserProvince];
                metaList.Add(province);

                var city = new UserMeta();
                city.MetaKey = AppConstants.UserCity;
                city.MetaValue = collection[AppConstants.UserCity];
                metaList.Add(city);

                var weibo = new UserMeta();
                weibo.MetaKey = AppConstants.UserWeibo;
                weibo.MetaValue = collection[AppConstants.UserWeibo];
                metaList.Add(weibo);

                var tencentWeibo = new UserMeta();
                tencentWeibo.MetaKey = AppConstants.UserTencentWeibo;
                tencentWeibo.MetaValue = collection[AppConstants.UserTencentWeibo];
                metaList.Add(tencentWeibo);

                var tencentQQ = new UserMeta();
                tencentQQ.MetaKey = AppConstants.UserTencentQQ;
                tencentQQ.MetaValue = collection[AppConstants.UserTencentQQ];
                metaList.Add(tencentQQ);

                var googlePlus = new UserMeta();
                googlePlus.MetaKey = AppConstants.UserGooglePlus;
                googlePlus.MetaValue = collection[AppConstants.UserGooglePlus];
                metaList.Add(googlePlus);

                var twitter = new UserMeta();
                twitter.MetaKey = AppConstants.UserTwitter;
                twitter.MetaValue = collection[AppConstants.UserTwitter];
                metaList.Add(twitter);

                var facebook = new UserMeta();
                facebook.MetaKey = AppConstants.UserFacebook;
                facebook.MetaValue = collection[AppConstants.UserFacebook];
                metaList.Add(facebook);

                user.UserMeta = metaList;
                user = _userMetaService.Create(user);

                try
                {
                    unitOfWork.SaveChanges();
                    unitOfWork.Commit();

                    var result = new LoginMessageViewModel();
                }
                catch (Exception ex)
                {
                    LoggingService.Error(ex);
                }

                return View(user);
            }
        }
Example #2
0
        public JsonResult LoginForJson(LoginViewModel model)
        {
            var result = new LoginMessageViewModel();
            result.Result = "false";
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var username = model.UserName;
                var password = model.Password;
                try
                {
                    if (ModelState.IsValid)
                    {
                        var user = new User();
                        var loginAttemptStatus = UserService.ValidateUser(username, password, 3);
                        if (loginAttemptStatus == LoginAttemptStatus.LoginSuccessful)
                        {
                            user = UserService.GetUser(username);
                            if (user.ActivationKey.IsNullEmpty())
                            {
                                FormsAuthentication.SetAuthCookie(username, model.RememberMe);
                                user.LastLoginDate = DateTime.UtcNow;

                                if (Url.IsLocalUrl(model.ReturnUrl) && model.ReturnUrl.Length > 1 && model.ReturnUrl.StartsWith("/") && !model.ReturnUrl.StartsWith("//") && !model.ReturnUrl.StartsWith("/\\"))
                                {
                                    result.Result = "true";
                                    result.Message = LocalizerHelper.Lang("登录成功");
                                    result.ReturnUrl = Url.Absolute(model.ReturnUrl);
                                }
                                result.Result = "true";
                                result.Message = LocalizerHelper.Lang("登录成功");
                                result.ReturnUrl = Url.Absolute("~/");
                            }
                            else
                            {
                                result.Result = "false";
                                result.Message = LocalizerHelper.Lang(string.Format("账号未激活,<a href=\"{0}\">现在激活</a>", Url.Absolute("~/Activation/")));
                            }
                        }
                        else
                        {
                            result.Result = "false";
                            result.Message = LocalizerHelper.Lang("账号或密码错误");
                        }
                    }
                }
                finally
                {
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                    }
                }
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }