public async Task <ActionResult> LogIn(AccountLogInViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                AppUserEntity user = null;

                //
                if (_RegularExpressions.SimpleEmail.IsMatch(model.EmailOrUsername))
                {
                    user = await _appUserManager.FindByEmailAsync(model.EmailOrUsername);
                }
                else
                {
                    user = await _appUserManager.FindByNameAsync(model.EmailOrUsername);
                }

                //
                if (user != null && user.Realms.Contains(Realm.AdminWebsite))
                {
                    _signInManager.InitialPersistenceState = model.RememberMe;

                    var result = await _signInManager.PasswordSignInAsync(
                        user.UserName,
                        model.Password,
                        model.RememberMe,
                        shouldLockout : true
                        );

                    switch (result)
                    {
                    case SignInStatus.LockedOut:
                    {
                        return(RedirectToAction("Locked", "Account"));
                    }

                    case SignInStatus.Success:
                    {
                        return(RedirectToLocal(returnUrl));
                    }
                    }
                }

                ModelState.AddModelError("credentials", GetLocalizedString <AreaResources>("InvalidCredentialsMessage"));
            }

            ViewBag.ReturnUrl = returnUrl;

            return(View(model));
        }
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // 这不会计入到为执行帐户锁定而统计的登录失败次数中
            // 在多次输入错误密码的情况下触发帐户锁定 shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.LoginName, model.Password, model.RememberMe, shouldLockout : true);

            switch (result)
            {
            case SignInStatus.Success:
                //不要信任returnUrl,只重定向本网站的returnUrl
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "无效的登录尝试。");
                return(View(model));
            }
        }
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // --- This doesn't count login failures towards account lockout
            // --- To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                var subscSrv = ServiceManager.GetService <ISubscriptionService>();
                var token    = await subscSrv.GetUserTokenByEmailAsync(model.Email);

                CreateAuthenticationTicket(model.RememberMe, token.UserId, token.UserName, token.IsServiceUser, token.SubscriptionId, token.IsSubscriptionOwner);
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            // ReSharper disable once RedundantCaseLabel
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", Res.Error_InvalidLogin);
                return(View(model));
            }
        }
示例#4
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            AppSignInManager signInManager = HttpContext.GetOwinContext().Get <AppSignInManager>();
            AppUserMananger  userManager   = HttpContext.GetOwinContext().GetUserManager <AppUserMananger>();

            // これは、アカウント ロックアウトの基準となるログイン失敗回数を数えません。
            // パスワード入力失敗回数に基づいてアカウントがロックアウトされるように設定するには、shouldLockout: true に変更してください。
            var result = await signInManager.PasswordSignInAsync(model.UserID, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "無効なログイン試行です。");
                return(View(model));
            }
        }
示例#5
0
        public async Task <ActionResult> LogIn(LogInViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
示例#6
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : true);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                ModelState.AddModelError("", "User locked out.");
                return(View(model));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
        public async Task <IActionResult> Login(LoginAccount account, string returnTo)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(
                    userName : account.UserName,
                    password : account.Password,
                    isPersistent : account.RememberMe,
                    lockoutOnFailure : false);


                if (result.Succeeded)
                {
                    return(RedirectToLocal(returnTo));
                }

                if (result.RequiresTwoFactor)
                {
                    return(RedirectToRoute("GetSendCode", new { returnTo, rememberMe = account.RememberMe }));
                }

                if (result.IsLockedOut)
                {
                    return(View("LockOut"));
                }

                if (result.IsNotAllowed)
                {
                    if (_userManager.Options.SignIn.RequireConfirmedPhoneNumber)
                    {
                        if (!await _userManager.IsPhoneNumberConfirmedAsync(new User {
                            UserName = account.UserName
                        }))
                        {
                            ModelState.AddModelError(string.Empty, "شماره تلفن شما تایید نشده است.");

                            return(View(account));
                        }
                    }


                    if (_userManager.Options.SignIn.RequireConfirmedEmail)
                    {
                        if (!await _userManager.IsEmailConfirmedAsync(new User {
                            UserName = account.UserName
                        }))
                        {
                            ModelState.AddModelError(string.Empty, "آدرس اییل شما تایید نشده است.");

                            return(View(account));
                        }
                    }
                }
            }


            ModelState.AddModelError(string.Empty, "Invalid username or password");

            return(View(account));
        }
示例#8
0
 public async Task<SignInStatus> Login(AppSignInManager SignInManager, string loginName, string password, bool rememberMe, bool shouldLockout)
 {
     // 这不会计入到为执行帐户锁定而统计的登录失败次数中
     // 若要在多次输入错误密码的情况下触发帐户锁定,请更改为 shouldLockout: true
     var result = await SignInManager.PasswordSignInAsync(loginName, password, rememberMe, shouldLockout);
     return result;
 }
示例#9
0
        public async Task <IActionResult> OnPostAsync(string returnTo)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(
                    userName : Account.UserName,
                    password : Account.Password,
                    isPersistent : Account.RememberMe,
                    lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    return(RedirectToLocal(returnTo));
                }

                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("SendCode", new { returnTo, rememberMe = Account.RememberMe }));
                }

                if (result.IsLockedOut)
                {
                    // todo create lockout view
                    return(RedirectToPage("/LockOut"));
                }

                if (result.IsNotAllowed)
                {
                    if (_userManager.Options.SignIn.RequireConfirmedPhoneNumber)
                    {
                        if (!await _userManager.IsPhoneNumberConfirmedAsync(new User {
                            UserName = Account.UserName
                        }))
                        {
                            ModelState.AddModelError(string.Empty, "شماره تلفن شما تایید نشده است.");
                            return(RedirectToPage("/Login"));
                        }
                    }

                    if (_userManager.Options.SignIn.RequireConfirmedEmail)
                    {
                        if (!await _userManager.IsEmailConfirmedAsync(new User {
                            UserName = Account.UserName
                        }))
                        {
                            ModelState.AddModelError(string.Empty, "آدرس اییل شما تایید نشده است.");
                            return(RedirectToPage("/Login"));
                        }
                    }
                }
            }
            return(RedirectToPage());
        }
示例#10
0
        public async Task <string> Login(string email, string password)
        {
            var result = await _signInManager.PasswordSignInAsync(email, password, false, true);

            if (result.Succeeded)
            {
                var user  = _userManager.Users.SingleOrDefault(r => r.Email == email);
                var roles = await _userManager.GetRolesAsync(user);

                return(GenerateJwtToken(user, roles));
            }

            var message = result.IsLockedOut
                ? "Ваш аккаунт заблокирован. Пожалуйста, подождите 10 минут и попробуйте войти снова. Вы также можете сбросить пароль или обратиться к системному администратору."
                : "Неверный e-mail или пароль";

            throw new UnauthorizedAccessException(message);
        }
示例#11
0
        public IActionResult LoginClient([FromBody] dtClientLogin dt)
        {
            A2ZClientCredentials clientModel = new A2ZClientCredentials();

            clientModel.UserName = dt.UserName;
            clientModel.Password = dt.Password;
            var result = signInManager.PasswordSignInAsync(clientModel.UserName, clientModel.Password, true, false).GetAwaiter().GetResult();

            if (result.Succeeded)
            {
                Redirect("/Home/LoggedInUser");
                return(Json("Client logged in successfully."));
            }
            else
            {
                return(Json("Username or Password given is incorrect."));
            }
        }
示例#12
0
        public async Task<ActionResult> Login(LoginModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, true, true);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToAction(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View(model);
            }
        }
示例#13
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    //  ApplicationUser user = await _signInManager.UserManager.FindByEmailAsync(Input.Email);

                    //  user.LastLogin =  DateTime.Now;
                    //   await _signInManager.UserManager.UpdateAsync(user);

                    _logger.LogInformation("User logged in.");
                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
示例#14
0
        public async Task <ActionResult> Login(LoginModel model, string returnUrl)
        {
            returnUrl = null;
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:

                return(Redirect(returnUrl ?? "/Home/Index"));

            case SignInStatus.LockedOut:
                ModelState.AddModelError("", "此用户已被锁定");
                return(View(model));

            default:
                ModelState.AddModelError("", "用户名或密码错误");
                return(View(model));
            }
        }
示例#15
0
        //[HttpPost("sign-in", Name = "PostLogin")]
        public async Task <IActionResult> Login(LoginAccount account, string returnTo)
        {
            if (account.Email.IndexOf('@') > -1)
            {
                //Validate email format
                string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                    @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                    @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                Regex re = new Regex(emailRegex);
                if (!re.IsMatch(account.Email))
                {
                    ModelState.AddModelError("Email", "ایمیل معتبر نمیباشد");
                }
                if (ModelState.IsValid)
                {
                    var user = await _userManager.FindByEmailAsync(account.Email);

                    if (user != null)
                    {
                        var result = await _signInManager.PasswordSignInAsync(

                            userName : user.UserName,
                            password : account.Password,
                            isPersistent : account.RememberMe,
                            lockoutOnFailure : true);


                        if (result.Succeeded)
                        {
                            return(RedirectToLocal(returnTo));
                        }

                        if (result.RequiresTwoFactor)
                        {
                            return(RedirectToRoute("GetSendCode", new { returnTo, rememberMe = account.RememberMe }));
                        }

                        if (result.IsLockedOut)
                        {
                            // todo create lockout view
                            return(View("LockOut"));
                        }

                        if (result.IsNotAllowed)
                        {
                            if (_userManager.Options.SignIn.RequireConfirmedPhoneNumber)
                            {
                                if (!await _userManager.IsPhoneNumberConfirmedAsync(new User {
                                    UserName = account.Email
                                }))
                                {
                                    ModelState.AddModelError(string.Empty, "شماره تلفن شما تایید نشده است.");

                                    return(View(account));
                                }
                            }


                            if (_userManager.Options.SignIn.RequireConfirmedEmail)
                            {
                                if (!await _userManager.IsEmailConfirmedAsync(new User {
                                    UserName = account.Email
                                }))
                                {
                                    ModelState.AddModelError(string.Empty, "آدرس ایمیل شما تایید نشده است.");

                                    return(View(account));
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                //validate Username format
                string emailRegex = @"^[a-zA-Z0-9]*$";
                Regex  re         = new Regex(emailRegex);
                if (!re.IsMatch(account.Email))
                {
                    ModelState.AddModelError("Email", "نام کاربری معتبر نمیباشد");
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        var user = await _userManager.FindByNameAsync(account.Email);

                        if (user != null)
                        {
                            var result = await _signInManager.PasswordSignInAsync(

                                userName : account.Email,
                                password : account.Password,
                                isPersistent : account.RememberMe,
                                lockoutOnFailure : false);


                            if (result.Succeeded)
                            {
                                return(RedirectToLocal(returnTo));
                            }

                            if (result.RequiresTwoFactor)
                            {
                                return(RedirectToRoute("GetSendCode", new { returnTo, rememberMe = account.RememberMe }));
                            }

                            if (result.IsLockedOut)
                            {
                                // todo create lockout view
                                return(View("LockOut"));
                            }

                            if (result.IsNotAllowed)
                            {
                                if (_userManager.Options.SignIn.RequireConfirmedPhoneNumber)
                                {
                                    if (!await _userManager.IsPhoneNumberConfirmedAsync(new User {
                                        UserName = account.Email
                                    }))
                                    {
                                        ModelState.AddModelError(string.Empty, "شماره تلفن شما تایید نشده است.");

                                        return(View(account));
                                    }
                                }


                                if (_userManager.Options.SignIn.RequireConfirmedEmail)
                                {
                                    if (!await _userManager.IsEmailConfirmedAsync(new User {
                                        UserName = account.Email
                                    }))
                                    {
                                        ModelState.AddModelError(string.Empty, "آدرس ایمیل شما تایید نشده است.");

                                        return(View(account));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            ModelState.AddModelError(string.Empty, "نام کاربری و یا کلمه‌ی عبور وارد شده معتبر نیستند.");

            return(View(account));
        }
示例#16
0
 public Task <SignInResult> AdminLogin(User user, string password)
 {
     return(_signInManager.PasswordSignInAsync(user, password, true, true));
 }