Beispiel #1
0
        public async Task <LoginResponseModel> Login(LoginDto loginDto)
        {
            User RequesterUser = null;

            // fetch either username or email
            if (loginDto.UserName != null)
            {
                RequesterUser = await _userManager.FindByNameAsync(loginDto.UserName);
            }

            if (loginDto.Email != null)
            {
                RequesterUser = await _userManager.FindByEmailAsync(loginDto.Email);
            }

            if (RequesterUser == null)
            {
                throw new LoginNotValidException();
            }

            // sign user in
            await _signInManager.SignOutAsync();

            SignInResult result = await _signInManager.PasswordSignInAsync(RequesterUser, loginDto.Password, false, false);

            return(new LoginResponseModel(result.Succeeded, result.ToString()));
        }
Beispiel #2
0
        public string SignIn(string username, string password)
        {
            SignInResult res = _unitOfWork.Users.SignIn(username, password);

            if (res == SignInResult.Successful)
            {
                var user = _unitOfWork.Users.GetByUsername(username);
                Session.Add("CurrentUser", user);
                Session.Add("CurrentUserId", user.Id);
            }

            return(res.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// PasswordSignInAsync
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <param name="isPersistent"></param>
        /// <param name="lockoutOnFailure"></param>
        /// <returns></returns>
        public override async Task <SignInResult> PasswordSignInAsync(TUser user, string password, bool isPersistent, bool lockoutOnFailure)
        {
            SignInResult result = await base.PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure).ConfigureAwait(false);

            ApplicationUser appUser = user as ApplicationUser;

            if (appUser != null) // We can only log an audit record if we can access the user object and it's ID
            {
                string ip = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();

                UserAudit auditRecord = null;
                Customer  customer    = null;
                switch (result.ToString())
                {
                case "Succeeded":
                    auditRecord = UserAudit.CreateAuditEvent(appUser.Id, UserAuditEventType.Login, ip);
                    if (!_db.Customers.Any(x => x.OwnerId.Contains(appUser.Id)))
                    {
                        customer = Customer.CreateCustomerEvent(ContactStatus.Rejected, appUser.FirstName + " " + appUser.LastName, appUser.Id, appUser.Email, appUser.AvatarURL);
                    }
                    break;

                case "Failed":
                    auditRecord = UserAudit.CreateAuditEvent(appUser.Id, UserAuditEventType.FailedLogin, ip);
                    break;
                }

                if (auditRecord != null)
                {
                    _db.UserAuditEvents.Add(auditRecord);
                    await _db.SaveChangesAsync().ConfigureAwait(false);
                }

                if (customer != null)
                {
                    _db.Customers.Add(customer);
                    await _db.SaveChangesAsync().ConfigureAwait(false);
                }
            }


            return(result);
        }