Exemplo n.º 1
0
        public ActionResult EditUser(UserFormModel model)
        {
            if (model != null)
            {
                if (ModelState.IsValid)
                {
                    User user = new User();
                    user.Id        = model.Id;
                    user.UserEmail = model.Email;

                    if (model.Birthdate > DateTime.Today)
                    {
                        ModelState.AddModelError("Birthdate", "Дата рождения не может быть в будущем");
                        return(View(model));
                    }

                    user.UserBirthdate = model.Birthdate;
                    user.RoleId        = model.RoleId;

                    if (model.Password != null)
                    {
                        user.UserPassword = HashManager.HashData(model.Password);
                    }

                    if (model.Id > 0)
                    {
                        this.accountService.UpdateUser(user);
                    }
                    else
                    {
                        this.accountService.AddUser(user);
                    }

                    return(RedirectToAction("AdminPanelUser"));
                }
                return(View(model));
            }
            return(View());
        }
Exemplo n.º 2
0
        public ActionResult Login(LoginFormModel model)
        {
            if (model != null)
            {
                if (ModelState.IsValid)
                {
                    UserModel user = this.accountService.GetUser(model.Email, HashManager.HashData(model.Password));

                    if (user == null)
                    {
                        ModelState.AddModelError("Email", "Неверный логин или пароль.");
                    }
                    else
                    {
                        Role role = this.accountService.GetRole(user.RoleId);

                        ClaimsIdentity claim = new ClaimsIdentity("ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
                        claim.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(), ClaimValueTypes.String));
                        claim.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, user.Email, ClaimValueTypes.String));
                        claim.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
                                                 "OWIN Provider", ClaimValueTypes.String));
                        if (role != null)
                        {
                            claim.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, role.RoleName, ClaimValueTypes.String)); //<---user.Role.RoleName
                        }
                        AuthenticationManager.SignOut();
                        AuthenticationManager.SignIn(new AuthenticationProperties
                        {
                            IsPersistent = true
                        }, claim);
                        return(RedirectToAction("MainPageCourses", "Course"));
                    }
                }
                return(View(model));
            }
            return(View());
        }
Exemplo n.º 3
0
        public ActionResult Register(UserRegistrationFormModel model)
        {
            if (model != null)
            {
                if (ModelState.IsValid)
                {
                    Role role         = this.accountService.GetRole("User");
                    User registration = new User();
                    registration.RoleId       = role.Id;
                    registration.UserEmail    = model.Email;
                    registration.UserPassword = HashManager.HashData(model.Password);

                    if (model.Birthdate > DateTime.Today)
                    {
                        ModelState.AddModelError("Birthdate", "Дата рождения не может быть в будущем");
                        return(View(model));
                    }

                    registration.UserBirthdate = model.Birthdate;

                    var state = this.accountService.AddUser(registration);

                    if (state.State == true)
                    {
                        EmailManager emailManager = new EmailManager();
                        emailManager.SendEmail($"Спасибо что зарегистрировались на сайте КурсоВод!<br/>Ваш логин: {model.Email};", "Registration on the kursovod.edu.ua", model.Email);

                        return(RedirectToAction("WindowAfterRegistration", "Account"));
                    }

                    ModelState.AddModelError("Email", "Пользователь с такими данными уже зарегистрирован.");
                }
                return(View(model));
            }
            return(View());
        }