ValidateUser() public method

public ValidateUser ( string username, string password ) : bool
username string
password string
return bool
Ejemplo n.º 1
0
        public void When_ValidateUser_is_called_with_a_valid_UserName_and_a_valid_Password_then_true_is_returned()
        {
            const string decryptedPassword = "******";
            var          user = UserCreator.CreateSingle();

            UserProcess
            .Expect(process =>
                    process.GetUserByLoginName(user.Login.LoginName))
            .Return(user)
            .Repeat.Once();
            UserProcess.Replay();

            CryptographyProcess
            .Expect(process => process.Decrypt(user.Login.Password))
            .Return(decryptedPassword)
            .Repeat.Once();
            CryptographyProcess.Replay();

            var result = CustomMembershipProvider.ValidateUser(user.Login.LoginName, decryptedPassword);

            UserProcess.VerifyAllExpectations();
            CryptographyProcess.VerifyAllExpectations();

            Assert.IsTrue(result);
        }
Ejemplo n.º 2
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (_membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") &&
                        !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 3
0
        private void btnLoginUser_Click(object sender, EventArgs e)
        {
            try
            {
                bool status = userProvider.ValidateUser(txtUsername.Text, txtPassword.Text);

                if (status)
                {
                    MessageBox.Show("Authenticated successfully", "User authentication", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Failed to authenticate user", "User authentication", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show("Failed to authenticate user, " + ex.Message, "User authentication", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Failed to authenticate user, " + ex.Message, "User authentication", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to authenticate user, " + ex.Message, "User authentication", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Ejemplo n.º 4
0
        public ActionResult LogIn(UserLoginView ULV, string returnUrl)
        {
            CustomMembershipProvider cs = new CustomMembershipProvider();

            if (ModelState.IsValid && cs.ValidateUser(ULV.LoginName, ULV.Password))
            {
                UserManager UM       = new UserManager();
                string      password = UM.GetUserPassword(ULV.LoginName);

                if (string.IsNullOrEmpty(password))
                {
                    ModelState.AddModelError("", "The user login or password provided is incorrect.");
                }
                else
                {
                    if (Crypto.VerifyHashedPassword(password, ULV.Password))
                    {
                        FormsAuthentication.SetAuthCookie(ULV.LoginName, false);
                        return(RedirectToAction("Welcome", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "The password provided is incorrect.");
                    }
                }
                string[] roless = UM.GetRolesForUser(ULV.LoginName);
            }

            // If we got this far, something failed, redisplay form
            return(View(ULV));
        }
        public ActionResult Index([Bind(Include = "UserName,Password")] LoginViewModel viewModel) // 引数にはLoginViewModelを受け取る
        {
            if (ModelState.IsValid)
            {
                if (_membershipProvider.ValidateUser(viewModel.UserName, viewModel.Password))
                {
                    //====================================================================================================
                    // ユーザー名とパスワードが正しければ、
                    // 認証Cookieを設定することによって認証状態を保持するようにする。
                    //====================================================================================================
                    // FormsAuthenticationクラス: ASP.NETに用意されているWebアプリケーションのフォーム認証サービスを管理するためのクラス。これを用いて認証状態を保持する。
                    // SetAuthCookieメソッド: ユーザー名をCookieに保持する。
                    // 認証Cookieがブラウザに保持されている間は、認証状態にあると判断される。
                    FormsAuthentication.SetAuthCookie(viewModel.UserName, false);

                    // 認証処理が終了したら、
                    // TodoesControllerのIndexアクションにリダイレクト。
                    return(RedirectToAction(nameof(Index), "Todoes"));
                }
            }

            // 入力が正しくなかったり、認証に失敗した場合は、
            // ログイン画面に戻すようにする。
            ViewBag.Message = "ログインに失敗しました。";  // ViewBagを用いて、ログイン失敗メッセージを指定
            return(View(viewModel));
        }
Ejemplo n.º 6
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            try
            {
                if (CustomMembershipProvider.ValidateUser(model.UserName, model.Password, model.Type))
                {
                    if (model.Type == (int)TypeUserEnum.Admin)
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") &&
                            !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return(Redirect(returnUrl));
                        }

                        return(RedirectToAction("Index", "ContentMenu"));
                    }
                    else
                    {
                        var loggingUser = _personReportService.GetUserByUserName(model.UserName);
                        Session["UserName"] = model.UserName;
                        Session["Name"]     = loggingUser.FullName;
                        Session["Person"]   = loggingUser.PersonId;
                        return(ModelState.JsonValidation(new { Success = true }));
                    }
                }
                else
                {
                    if (model.Type == (int)TypeUserEnum.Admin)
                    {
                        ModelState.AddModelError("Error", "Tên đăng nhập hoặc mật khẩu sai");
                        return(View(model));
                    }
                    else
                    {
                        return(ModelState.JsonValidation(new { Success = true, Error = "Tên đăng nhập hoặc mật khẩu sai" }));
                    }
                }
            }
            catch (Exception ex)
            {
                if (model.Type == (int)TypeUserEnum.Admin)
                {
                    ModelState.AddModelError("Error", ex.Message);
                    return(View("Login", model));
                }
                else
                {
                    return(ModelState.JsonValidation(new { Success = true, Error = ex.Message }));
                }
            }


            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 7
0
 public ActionResult Login(LoginModel model, string returnUrl)
 {
     if (ModelState.IsValid && _membership.ValidateUser(model.UserName, model.Password))
     {
         FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
         return(RedirectToLocal(returnUrl));
     }
     // If we got this far, something failed, redisplay form
     ModelState.AddModelError("", "The user name or password provided is incorrect.");
     return(View(model));
 }
Ejemplo n.º 8
0
 public ActionResult Index([Bind(Include = "UserName,Password")] LoginViewModel model)
 {
     if (ModelState.IsValid)
     {
         if (membershipProvider.ValidateUser(model.UserName, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.UserName, false);
             return(RedirectToAction("Index", "Articles"));
         }
     }
     return(View(model));
 }
Ejemplo n.º 9
0
        public void When_ValidateUser_is_called_with_an_invalid_UserName_then_false_is_returned()
        {
            UserProcess
            .Expect(process => process.GetUserByLoginName(Arg <string> .Is.Anything))
            .Return(null)
            .Repeat.Once();
            UserProcess.Replay();

            var result = CustomMembershipProvider.ValidateUser("userName", "password");

            Assert.IsFalse(result);
        }
Ejemplo n.º 10
0
        public bool ValidateUser(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentException("值不能为 null 或为空。", "userName");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentException("值不能为 null 或为空。", "password");
            }

            return(_provider.ValidateUser(userName, password));
        }
Ejemplo n.º 11
0
        public ActionResult Index([Bind(Include = "UserName,Password")] LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (membershipProvider.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, true);
                    return(RedirectToAction("Index", "Todoes"));
                }
            }

            ViewBag.Message = "ログインに失敗しました。";
            return(View(model));
        }
Ejemplo n.º 12
0
        public ActionResult Login(LoginModel model, string ReturnUrl = "")
        {
            CustomMembershipProvider customMembershipProvider = new CustomMembershipProvider();

            if (ModelState.IsValid)
            {
                if (customMembershipProvider.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    return(RedirectToLocal(ReturnUrl));
                }
            }
            ModelState.AddModelError("", "Error : El usuario o la clave no son válidos. ");
            return(View(model));
        }
Ejemplo n.º 13
0
 public ActionResult Login(LogOnViewModel viewModel, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         CustomMembershipProvider provider = new CustomMembershipProvider(_userService, _roleService);
         if (provider.ValidateUser(viewModel.Email, viewModel.Password))
         {
             FormsAuthentication.SetAuthCookie(viewModel.Email, viewModel.RememberMe);
             return(Redirect(returnUrl ?? Url.Action("Information", "Profile")));
         }
         else
         {
             ModelState.AddModelError("", "Incorrect login or password.");
         }
     }
     return(View(viewModel));
 }
Ejemplo n.º 14
0
        private bool ValidateLogOn(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName))
            {
                ModelState.AddModelError("username", "You must specify a username.");
            }
            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password", "You must specify a password.");
            }
            if (!_Provider.ValidateUser(userName, password))
            {
                ModelState.AddModelError("_FORM", "The username or password provided is incorrect.");
            }

            return(ModelState.IsValid);
        }
Ejemplo n.º 15
0
 public ActionResult Login(LogOnViewModel viewModel, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (provider.ValidateUser(viewModel.Email, viewModel.Password))
         //Проверяет учетные данные пользователя и управляет параметрами пользователей
         {
             FormsAuthentication.SetAuthCookie(viewModel.Email, viewModel.RememberMe);
             //Управляет службами проверки подлинности с помощью форм для веб-приложений
             if (Url.IsLocalUrl(returnUrl))
             {
                 return(Redirect(returnUrl));
             }
             return(RedirectToAction("Index", "Home"));
         }
         else
         {
             ModelState.AddModelError("", "Incorrect login or password.");
         }
     }
     return(View(viewModel));
 }
Ejemplo n.º 16
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            //if (!ModelState.IsValid)
            //{
            //    return View(model);
            //}

            CustomMembershipProvider customeProvider = new CustomMembershipProvider();

            if (customeProvider.ValidateUser(model.Email, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);

                return(Redirect("~/quantri/homepage"));//RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Tài khoản hoặc mật khẩu không chính xác !");
                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:
            //        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);
            //}
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Validates the user.
 /// </summary>
 /// <param name="login">The user login.</param>
 /// <param name="password">The user password.</param>
 public bool ValidateUser(string login, string password)
 {
     return provider.ValidateUser(login, password);
 }
Ejemplo n.º 18
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="password"></param>
 /// <returns></returns>
 public bool ValidateUser(string userName, string password)
 {
     return(_provider.ValidateUser(userName, password));
 }