Inheritance: IdentityUser
Esempio n. 1
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                bool isApprovedUser = _users.IsApprovedUser(model.Username);

                //DATA_MODELS.User user = _users.GetUserByName(model.Username).ProjectTo<DATA_MODELS.User>().FirstOrDefault();

                DATA_MODELS.User user = _users
                                        .GetUserByName(model.Username)
                                        .Select(u => new DATA_MODELS.User
                {
                    Id         = u.Id,
                    UserName   = u.UserName,
                    Name       = u.Name,
                    Email      = u.Email,
                    IsApproved = u.IsApproved
                })
                                        .FirstOrDefault();

                if (user == null)
                {
                    ModelState.AddModelError(string.Empty, "No such user exists.");
                    return(View(model));
                }
                if (isApprovedUser == false)
                {
                    ModelState.AddModelError(string.Empty, "You must wait to be approved by administrator.");
                    return(View(model));
                }
                var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, lockoutOnFailure : false);

                if (result.Succeeded && isApprovedUser)
                {
                    _logger.LogInformation("User logged in.");
                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToAction(nameof(Lockout)));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
        protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
        {
            this.CurrentUser = this.Data.Users.All()
                                   .Where(u => u.UserName == requestContext.HttpContext.User.Identity.Name)
                                   .FirstOrDefault();

            return base.BeginExecute(requestContext, callback, state);
        }
Esempio n. 3
0
        public bool IsApprovedUser(string username)
        {
            DATA_MODELS.User user = this.db.Users.FirstOrDefault(u => u.UserName == username);

            if (user == null)
            {
                return(false);
            }

            return(user.IsApproved);
        }
Esempio n. 4
0
        public void Approve(string id)
        {
            DATA_MODELS.User user = this.db.Users.Find(id);

            if (user == null)
            {
                return;
            }

            user.IsApproved = true;

            this.db.SaveChanges();
        }
Esempio n. 5
0
        public void Remove(string id)
        {
            DATA_MODELS.User user = this.db.Users.Find(id);

            if (user == null)
            {
                return;
            }

            this.db.Remove(user);

            this.db.SaveChanges();
        }
Esempio n. 6
0
        public bool ChangeData(string id, string name, string email)
        {
            DATA_MODELS.User user = this.db.Users.FirstOrDefault(u => u.Id == id);

            if (user == null)
            {
                return(false);
            }

            user.Name  = name;
            user.Email = email;

            this.db.SaveChanges();

            return(true);
        }
Esempio n. 7
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                var err = ModelState.ToBadRequestErrorModel();
                return(BadRequest(ModelState.ToBadRequestErrorModel()));
            }

            bool isApprovedUser = _users.IsApprovedUser(model.Username);

            DATA_MODELS.User user = _users
                                    .GetUserByName(model.Username)
                                    .Select(u => new DATA_MODELS.User
            {
                Id         = u.Id,
                UserName   = u.UserName,
                Name       = u.Name,
                Email      = u.Email,
                IsApproved = u.IsApproved
            })
                                    .FirstOrDefault();

            if (user == null)
            {
                ModelState.AddModelError(string.Empty, "No such user exists.");
                return(BadRequest(ModelState.ToBadRequestErrorModel()));
            }

            if (isApprovedUser == false)
            {
                ModelState.AddModelError(string.Empty, "You must wait to be approved by administrator.");
                return(StatusCode(401, ModelState.ToUnauthorizedErrorModel()));
            }

            var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, lockoutOnFailure : false);

            if (result.Succeeded && isApprovedUser)
            {
                _logger.LogInformation("User logged in.");
                return(Ok(user));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return(BadRequest(ModelState.ToBadRequestErrorModel()));
            }
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var user = new User(model.UserName);
                var result = await this.UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return this.RedirectToAction("Index", "Home");
                }

                this.AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return this.View(model);
        }
 private async Task SignInAsync(User user, bool isPersistent)
 {
     this.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await this.UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     this.AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return this.RedirectToAction("Manage");
            }

            if (this.ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await this.AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return this.View("ExternalLoginFailure");
                }

                var user = new User(model.UserName);
                var result = await this.UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await this.UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await this.SignInAsync(user, isPersistent: false);
                        return this.RedirectToLocal(returnUrl);
                    }
                }

                this.AddErrors(result);
            }

            this.ViewBag.ReturnUrl = returnUrl;
            return this.View(model);
        }
Esempio n. 11
0
 private async Task SignInAsync(User user, bool isPersistent)
 {
     this.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
     this.AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(this.UserManager));
 }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var user = new User { UserName = model.Username, Email = model.Email };
                var result = await this.UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await this.SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return this.RedirectToAction("Index", "Home");
                }
                this.AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return this.View(model);
        }