コード例 #1
0
ファイル: AccountController.cs プロジェクト: zszqwe/FNHMVC
        public async Task <ActionResult> Register(UserFormModel model)
        {
            if (ModelState.IsValid)
            {
                var command = new UserRegisterCommand
                {
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Email     = model.Email,
                    Password  = model.Password,
                    Activated = true,
                    RoleId    = (Int32)UserRoles.User
                };

                IEnumerable <ValidationResult> errors = commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                if (ModelState.IsValid)
                {
                    var result = commandBus.Submit(command);
                    if (result.Success)
                    {
                        var        user    = this.userRepository.Get(x => x.Email.ToUpper() == command.Email.ToUpper() && Md5Encrypt.Md5EncryptPassword(command.Password) == x.PasswordHash);
                        FNHMVCUser appUser = new FNHMVCUser()
                        {
                            Id       = user.UserId,
                            RoleName = Enum.GetName(typeof(UserRoles), user.RoleId),
                            UserName = user.DisplayName
                        };
                        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                        AuthenticationManager.SignIn(new AuthenticationProperties()
                        {
                            IsPersistent = true
                        }, await appUser.GenerateUserIdentityAsync(userManager));
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "An unknown error occurred.");
                    }
                }
                return(View(model));
            }

            return(View(model));
        }
コード例 #2
0
ファイル: AccountController.cs プロジェクト: zszqwe/FNHMVC
        public async Task <ActionResult> Login(LogOnFormModel form, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = this.userRepository.Get(x => x.Email.ToUpper() == form.Email.ToUpper() && Md5Encrypt.Md5EncryptPassword(form.Password) == x.PasswordHash);
                if (user != null)
                {
                    FNHMVCUser appUser = new FNHMVCUser(user);
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                    AuthenticationManager.SignIn(new AuthenticationProperties()
                    {
                        IsPersistent = true, RedirectUri = returnUrl
                    }, await appUser.GenerateUserIdentityAsync(userManager));
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            return(View(form));
        }