public async Task SendAsync(AppUserLoginEvent loginEvent) { Template template = await this._templateProvider.GetTemplateAsync(TemplateName); template.AddReplacement("user-name", loginEvent.User.UserName); template.AddReplacement("user-agent", loginEvent.UserAgent ?? "Onbekend"); template.AddReplacement("timestamp", loginEvent.Timestamp.ToString("F")); template.AddReplacement("ip-address", loginEvent.IPAddress); await this._mailService.SendAsync(loginEvent.User.Email, template); }
private async Task HandleLoginCore(AppUser user, HttpContext httpContext) { if (this.HasTrustedCookie(user, httpContext.Request)) { this._logger.LogInformation($"User #{user.Id} logs in with a trusted cookie"); // Refresh cookie this.SetTrustedResponseCookie(user, httpContext); return; } string ipAddress = httpContext.Connection.RemoteIpAddress?.ToString(); if (String.IsNullOrEmpty(ipAddress)) { this._logger.LogWarning($"User #{user.Id} logs in - no IP address???"); return; } string userAgent = httpContext.Request.Headers["User-Agent"]; if (await this.ExistsInDatabase(ipAddress, userAgent, user.Id)) { this._logger.LogInformation($"User #{user.Id} logs in, based on IP address [{ipAddress}] and user agent [{userAgent}] this login is trusted"); // Refresh cookie this.SetTrustedResponseCookie(user, httpContext); return; } var loginEvent = new AppUserLoginEvent { IPAddress = ipAddress, UserAgent = userAgent, Timestamp = DateTime.Now, User = user, UserId = user.Id }; this._appUserLoginEventRepository.Add(loginEvent); await this._appUserLoginEventRepository.SaveChangesAsync(); this.SetTrustedResponseCookie(user, httpContext); this._logger.LogInformation($"User #{user.Id} logs in, based on IP address [{ipAddress}] and user agent [{userAgent}] this login is new"); if (user.Preferences.EnableLoginNotifications && user.EmailConfirmed) { this._logger.LogInformation($"User #{user.Id} logs in, sending notification for new login"); await this._appUserLoginEventMailer.SendAsync(loginEvent); } }
public void Add(AppUserLoginEvent loginEvent) { this._appDbContext.Set <AppUserLoginEvent>().Add(loginEvent); }