Пример #1
0
        public ActionResult Login(AccountLoginInputModel input, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var result = this.accountAdapter.LoginUser(input.UserName, input.Password);
                if (result.StatusCode == 200)
                {
                    // set auth cookie
                    FormsAuthentication.SetAuthCookie(result.Result.Username, input.RememberMe);

                    // allow cross-browser auth cookie (IE8)
                    Response.AddHeader("p3p",
                                       "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");

                    // redirect the user
                    if (String.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect("/"));
                    }
                    else
                    {
                        return(Redirect(returnUrl));
                    }
                }

                // process failure
                var error = result.Errors.First();
                ModelState.AddModelError(error.MemberNames.First(), error.ErrorMessage);
            }
            return(View(new AccountLoginModel()
            {
                Input = input
            }));
        }
Пример #2
0
        public async Task <SignInResult> Register(AccountRegisterInputModel model)
        {
            var user = new PandaUser {
                UserName = model.Username, Email = model.Email
            };

            var registerResult = await this.userManager.CreateAsync(user, model.Password);

            if (!registerResult.Succeeded)
            {
                throw new Exception(string.Join(Environment.NewLine, registerResult.Errors));
            }

            var adminRoleExists = await this.roleManager.RoleExistsAsync("Admin");

            if (!adminRoleExists)
            {
                await this.roleManager.CreateAsync(new PandaUserRole { Name = "Admin" });
            }

            var userRoleExists = await this.roleManager.RoleExistsAsync("User");

            if (!userRoleExists)
            {
                await this.roleManager.CreateAsync(new PandaUserRole { Name = "User" });
            }

            var usersCount = this.userManager.Users.Count();

            if (usersCount == 1)
            {
                await this.userManager.AddToRoleAsync(user, "Admin");
            }
            else
            {
                await this.userManager.AddToRoleAsync(user, "User");
            }

            var loginModel = new AccountLoginInputModel
            {
                Username   = model.Username,
                Password   = model.Password,
                RememberMe = false
            };

            var result = await this.Login(loginModel);

            return(result);
        }
Пример #3
0
        public async Task <IActionResult> Login(AccountLoginInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var result = await this.accountService.Login(model);

            if (!result.Succeeded)
            {
                return(this.View(model));
            }

            return(this.Redirect("/"));
        }
Пример #4
0
        public ActionResult Login(AccountLoginInputModel input)
        {
            if (ModelState.IsValid)
            {
                if (_authentication.Authenticate(input.Username, input.Password))
                {
                    _currentUser.Login(input.Username);
                    if (!String.IsNullOrEmpty(input.ReturnUrl) && Url.IsLocalUrl(input.ReturnUrl))
                    {
                        return Redirect(input.ReturnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }

                ModelState.AddModelError("", "Invalid username or password.");
            }
            return Login(input.ReturnUrl);
        }
Пример #5
0
        public async Task <SignInResult> Login(AccountLoginInputModel model)
        {
            var result = await this.signInManager.PasswordSignInAsync(model.Username, model.Password, false, lockoutOnFailure : true);

            return(result);
        }