Exemple #1
0
        public override async Task <SignInResult> PasswordSignInAsync(TUser user, string password, bool isPersistent, bool lockoutOnFailure)
        {
            var result = await base.PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure);

            var appUser = user as IdentityUser;

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

                UserLogAudit auditRecord = null;

                switch (result.ToString())
                {
                case "Succeeded":
                    auditRecord = new UserLogAudit
                    {
                        AuditEvent = UserAuditEventType.Login,
                        IpAddress  = ip,
                        UserId     = appUser.Id
                    };
                    break;

                case "Failed":
                    auditRecord = new UserLogAudit
                    {
                        AuditEvent = UserAuditEventType.FailedLogin,
                        IpAddress  = ip,
                        UserId     = appUser.Id
                    };
                    break;
                }

                if (auditRecord != null)
                {
                    _db.UserLogAudits.Add(auditRecord);
                    await _db.SaveChangesAsync();
                }
            }

            return(result);
        }
Exemple #2
0
        public override async Task SignOutAsync()
        {
            await base.SignOutAsync();

            var user = await _userManager.GetUserAsync(_contextAccessor.HttpContext.User) as IdentityUser;

            if (user != null)
            {
                var ip = _contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();

                var auditRecord = new UserLogAudit
                {
                    AuditEvent = UserAuditEventType.LogOut,
                    IpAddress  = ip,
                    UserId     = user.Id
                };
                _db.UserLogAudits.Add(auditRecord);
                await _db.SaveChangesAsync();
            }
        }