Exemplo n.º 1
0
        public async Task <IActionResult> Login(AccountLoginVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var loginResult = await service.LoginAsync(vm);

            if (loginResult.Succeeded)
            {
                if (!string.IsNullOrEmpty(vm.ReturnUrl))
                {
                    return(Redirect(vm.ReturnUrl));
                }
                else
                {
                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
            }
            else
            {
                ModelState.AddModelError(nameof(AccountLoginVM.UserName), "Invalid username and/or password.");
                return(View(vm));
            }
        }
        public async Task <IActionResult> LoginAsync(AccountLoginVM viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            // Check if credentials is valid (and set auth cookie)
            var success = await accountService.TryLoginAsync(viewModel);

            if (!success)
            {
                // Show error
                ModelState.AddModelError(nameof(AccountLoginVM.UserName), "Login failed");
                return(View(viewModel));
            }

            // Redirect user
            if (string.IsNullOrWhiteSpace(viewModel.ReturnUrl))
            {
                return(RedirectToAction(nameof(Members)));
            }
            else
            {
                return(Redirect(viewModel.ReturnUrl));
            }
        }
        public async Task <IActionResult> Login([FromBody] AccountLoginVM viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { modelState = false }));
            }

            // Check if credentials is valid (and set auth cookie)
            var result = await service.TryLoginAsync(viewModel);

            if (!result.Succeeded)
            {
                // Show error
                ModelState.AddModelError(nameof(AccountLoginVM.Username), "Login failed");
                return(Json(new { success = false, loginResponseText = "Email eller lösenord stämmer inte" }));
            }

            var x         = service.GetUserIdAndFNameFromDB(viewModel);
            var userID    = x[0].Id;
            var firstName = x[0].FirstName;

            // Redirect user
            if (string.IsNullOrWhiteSpace(viewModel.ReturnUrl))
            {
                return(Json(new { success = true, responseText = "Inloggning Lyckades", userId = userID, firstName = firstName /*, JsonRequestBehavior.AllowGet*/ }));
            }

            else
            {
                return(Redirect(viewModel.ReturnUrl));
            }
        }
Exemplo n.º 4
0
        public IActionResult Login(AccountLoginVM login)
        {
            //simulate a transaction
            TempData[$"account_{login.AccountId}"] = true;

            return(RedirectToAction("TransactionHistory", new { accountId = login.AccountId, page = 0 }));
        }
Exemplo n.º 5
0
        public ActionResult Login()
        {
            AccountLoginVM model = new AccountLoginVM();

            TryUpdateModel(model);

            AuthenticationManager.AuthenticateUser(model.Username, model.Password);

            if (AuthenticationManager.LoggedUser == null)
            {
                ModelState.AddModelError(String.Empty, "Invalid username or password.");
                return(View(model));
            }

            if (model.IsRemembered)
            {
                CookieService.CreateCookie();
            }

            if (!String.IsNullOrEmpty(model.RedirectUrl))
            {
                return(Redirect(model.RedirectUrl));
            }

            return(this.RedirectToAction <ContactsController>(c => c.List()));
        }
Exemplo n.º 6
0
        public ActionResult Login(AccountLoginVM user)
        {
            try
            {
                var u = db.Users.SingleOrDefault(r => r.Email == user.Email && r.Password == user.Password);


                if (u != null)
                {
                    FormsAuthentication.SetAuthCookie(u.Id.ToString(), false);
                    Session["id"] = u.Id;
                    return(RedirectToAction("Index", "Doctor"));
                }
                else
                {
                    ViewBag.page = "post";
                    ModelState.AddModelError("", "Incorrect username or password. Please confirm your login information.");
                }

                return(View());
            }
            catch (Exception e)
            {
                ViewBag.ExceptionMessage = e.Message;
            }
            return(View("~/Views/Errors/Details.cshtml"));
        }
Exemplo n.º 7
0
        public IActionResult Login()
        {
            HttpContext.Session.Remove("UserId");
            AccountLoginVM vm = new AccountLoginVM();

            return(View(vm));
        }
        public async Task <IActionResult> Login(AccountLoginVM viewModel, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            // Skapa DB-schemat
            //await identityContext.Database.EnsureCreatedAsync();

            // Create user
            //var user = new IdentityUser("admin");
            //var result = await userManager.CreateAsync(user, "Pencil69!");

            var result = await signInManager.PasswordSignInAsync(
                viewModel.Username, viewModel.Password, false, false);

            if (!result.Succeeded)
            {
                ModelState.AddModelError(nameof(AccountLoginVM.Username),
                                         "Incorrect login credentials");
                return(View(viewModel));
            }

            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                return(RedirectToAction(nameof(AdminController.Index)));
            }
            else
            {
                return(Redirect(returnUrl));
            }
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Login(AccountLoginVM viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            // Check if credentials is valid (and set auth cookie)
            if (!await accountService.TryLoginAsync(viewModel))
            {
                // Show login error
                ModelState.AddModelError(nameof(AccountLoginVM.Username), "Invalid credentials");
                return(View(viewModel));
            }


            // Redirect user
            if (string.IsNullOrWhiteSpace(viewModel.ReturnUrl))
            {
                return(RedirectToAction(nameof(CaseController.Index), "case"));
            }
            //return RedirectToAction("Index", "Case", "case");
            else
            {
                return(Redirect(viewModel.ReturnUrl));
            }
        }
Exemplo n.º 10
0
        public ActionResult Login(AccountLoginVM vm)
        {
            try
            {
                string password = Hasher.ToHashedStr(vm.Password);
                var    u        = context.Users.SingleOrDefault(q => q.Email.ToLower() == vm.Email.ToLower() && q.Password == password);

                // if username(email) and password are correct
                if (u != null && u.Role == "patient")
                {
                    FormsAuthentication.SetAuthCookie(u.Id.ToString(), false);

                    return(RedirectToAction("Index", "Patient", new { Id = u.Id }));
                }
                else if (u != null && u.Role == "admin")
                {
                    FormsAuthentication.SetAuthCookie(u.Id.ToString(), false);

                    return(RedirectToAction("Index", "Admin"));
                }
                else
                {
                    ModelState.AddModelError("", "Incorrect username or password. Please confirm your login information.");
                }

                return(View("Login"));
            }
            catch (Exception e)
            {
                ViewBag.ExceptionMessage = e.Message;
            }
            return(View("~/Views/Errors/Details.cshtml"));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Login(AccountLoginVM model, string redirectUrl)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);

                if (result.Succeeded)
                {
                    if (!string.IsNullOrEmpty(redirectUrl))
                    {
                        return(LocalRedirect(redirectUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }

                ModelState.AddModelError(string.Empty, "Login failed");
            }

            ViewBag.Title = "Log in";

            return(View(model));
        }
Exemplo n.º 12
0
        public ActionResult Login(string redirectUrl)
        {
            AccountLoginVM model = new AccountLoginVM();

            model.RedirectUrl = redirectUrl;
            return(View(model));
        }
Exemplo n.º 13
0
        public IActionResult Login(string returnUrl)
        {
            var model = new AccountLoginVM {
                ReturnUrl = returnUrl
            };

            return(View(model));
        }
Exemplo n.º 14
0
        public ActionResult Login(AccountLoginVM model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                switch (model.User)
                {
                case "Devin":
                    break;

                case "Andrea":
                    break;

                default:
                    ModelState.AddModelError("", "The user provided is incorrect.");
                    return(View(model));
                }

                if (model.Password == "riley")
                {
                    FormsAuthentication.SetAuthCookie(model.User, true);

                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }

                    return(RedirectToAction("Index", "Home"));
                }

                ModelState.AddModelError("", "The password provided is incorrect.");

                /*if (Membership.ValidateUser(model.UserName, model.Password))
                 * {
                 *      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                 *
                 *      if (Url.IsLocalUrl(returnUrl))
                 *              return Redirect(returnUrl);
                 *
                 *      return RedirectToAction("LoggedIn");
                 * }
                 * else
                 * {
                 *      MembershipUser user = Membership.GetUser(model.UserName);
                 *      if (user == null)
                 *              ModelState.AddModelError("", "The user name provided does not exist.");
                 *      else if (!user.IsApproved)
                 *              ModelState.AddModelError("", "This account has not yet been approved.");
                 *      else if (user.IsLockedOut)
                 *              ModelState.AddModelError("", "This account has been locked out.");
                 *      else
                 *              ModelState.AddModelError("", "The password provided is incorrect.");
                 * }*/
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 15
0
 public async Task <SignInResult> TryLoginAsync(AccountLoginVM viewModel)
 {
     // Try to sign user
     return(await signInManager.PasswordSignInAsync(
                viewModel.Username,
                viewModel.Password,
                isPersistent : false,
                lockoutOnFailure : false));
 }
 public AspNetUsers[] GetUserIdAndFNameFromDB(AccountLoginVM id)
 {
     return(context.AspNetUsers
            .Where(p => p.UserName.Contains(id.Username))
            .Select(p => new AspNetUsers
     {
         Id = p.Id,
         FirstName = p.FirstName
     }).ToArray());
 }
Exemplo n.º 17
0
        public async Task <bool> TryLoginAsync(AccountLoginVM viewModel)
        {
            var result = await signInManager.PasswordSignInAsync(
                viewModel.UserName,
                viewModel.Password,
                isPersistent : false,
                lockoutOnFailure : false);

            return(result.Succeeded);
        }
Exemplo n.º 18
0
        public async Task <bool> TryLoginAsync(AccountLoginVM viewModel)
        {
            //// Create DB schema (first time)
            var createSchemaResult = await identityContext.Database.EnsureCreatedAsync();

            //// Create a hard coded user (first time)
            var createResult = await userManager.CreateAsync(new IdentityUser("user"), "Password_123");

            var loginResult = await signInManager.PasswordSignInAsync(viewModel.Username, viewModel.Password, false, false);

            return(loginResult.Succeeded);
        }
Exemplo n.º 19
0
        public ActionResult Login(AccountLoginVM viewModel)
        {
            AuthenticationManager.Authenticate(viewModel.Username, viewModel.Password);

            if (AuthenticationManager.LoggedUser == null)
            {
                ModelState.AddModelError("AuthenticationFailed", "WrongUsernameOrPassword");

                return(View(viewModel));
            }

            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 20
0
        public ActionResult Login(AccountLoginVM model)
        {
            AuthenticationService.AuthenticateUser(model.Username, model.Password);

            if (AuthenticationService.IsLogged)
            {
                if (string.IsNullOrWhiteSpace(model.RedirectUrl))
                {
                    return(RedirectToAction("Index", "Home"));
                }

                return(Redirect(model.RedirectUrl));
            }

            return(View());
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Login(AccountLoginVM account)
        {
            if (!ModelState.IsValid)
            {
                return(View(account));
            }

            var result = await _service.SignInAsync(account.UserName, account.Password);

            if (!result)
            {
                ModelState.AddModelError(nameof(AccountLoginVM.UserName), "Invalid login credentials");
                return(View(account));
            }

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Login(AccountLoginVM model)
        {
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(model.UserName, model.PassWord, false, false);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError(nameof(AccountLoginVM.UserName), "Felaktigt användarnamn eller lösenord.");
                }
                else
                {
                    return(Redirect("/Home/Index"));  //todo Kolla om det går att byta ut den magiska strängen
                }
            }
            return(View(model));
        }
Exemplo n.º 23
0
        public AccountLoginVM GetLoginVM(IIdentity user)
        {
            var ret = new AccountLoginVM
            {
                IsLoggedIn = user.IsAuthenticated
            };

            if (user.IsAuthenticated)
            {
                ret.Username = user.Name;
            }
            else
            {
            }

            return(ret);
        }
Exemplo n.º 24
0
        public async Task <IActionResult> Login(AccountLoginVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }


            var result = await service.TryLoginUser(vm);

            if (!result.Succeeded)
            {
                ModelState.AddModelError(string.Empty, "Användarnamn och/eller lösenord är felaktigt");
                return(View(vm));
            }
            return(RedirectToAction("Main", "Functions"));
        }
Exemplo n.º 25
0
        public ActionResult Login()
        {
            AccountLoginVM model = new AccountLoginVM();

            TryUpdateModel(model);

            AuthenticationService.AuthenticateConsultant(model.Username, model.Password);
            if (AuthenticationService.LoggedConsultant != null)
            {
                if (!String.IsNullOrEmpty(model.RedirectUrl))
                {
                    return(Redirect(model.RedirectUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }
            return(View(model));
        }
Exemplo n.º 26
0
        public async Task <IActionResult> Login(AccountLoginVM viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var result = await signInManager.PasswordSignInAsync(viewModel.UserName, viewModel.Password, false, false);

            if (!result.Succeeded)
            {
                ModelState.AddModelError(nameof(AccountLoginVM.UserName), "Wrong username or password");
                return(View());
            }
            ;

            return(RedirectToAction(nameof(AccountController.UserPage), "Account", new { title = $"Userpage {viewModel.UserName}" }));
        }
Exemplo n.º 27
0
        public ActionResult Login()
        {
            AccountLoginVM model = new AccountLoginVM();

            TryUpdateModel(model);

            AuthenticationService.Authenticate(model.Username, model.Password);

            if (AuthenticationService.LoggedUser != null)
            {
                return(RedirectToAction("List", "Courses"));
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password");
                return(View(model));
            }
        }
Exemplo n.º 28
0
        public async Task <IActionResult> Login(AccountLoginVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var result = await service.TryLoginCompanyAsync(vm);

            if (!result.Succeeded)
            {
                ModelState.AddModelError("", "Invalid input");
                return(View());
            }
            else
            {
                return(RedirectToAction(nameof(Index), "Home"));
            }
        }
Exemplo n.º 29
0
        internal async Task <IdentityResult> createAccount(AccountLoginVM vm)
        {
            var user = new MyIdentityUser {
                UserName = vm.UserName
            };
            var result = await userManager.CreateAsync(user, vm.Password);

            if (result.Succeeded)
            {
                if (!await roleManager.RoleExistsAsync("User"))
                {
                    var role = new IdentityRole("user");
                    var res  = await roleManager.CreateAsync(role);
                }

                await userManager.AddToRoleAsync(user, "user");
            }

            return(result);
        }
Exemplo n.º 30
0
        public async Task <IActionResult> Login(AccountLoginVM viewModel, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }


            var result = await signInManager.PasswordSignInAsync(viewModel.Email, viewModel.Password, false, false);

            if (!result.Succeeded)
            {
                ModelState.AddModelError(nameof(AccountLoginVM.Password), "Ogiltig inloggning");
                return(View(viewModel));
            }

            //return RedirectToAction(nameof(SplitController.Index), nameof(SplitController).Replace("Controller", ""));
            return(RedirectToLocal(returnUrl));
        }