ASP.NET Core 5.0 - Preferred UserAgents
This article will describe how to capture a user agent string and store it with a preferred property. I will assume you have downloaded the ASP.NET Core 5.0 - Homegrown Analytics Project or created a new ASP.NET Core 5.0 Razor Pages project. See Tutorial: Get started with Razor Pages in ASP.NET Core.
Homegrown Analytics Project and Article Series
The project is feature complete and will increment the version with updates. The details page includes the version change log. This project is designed and organized to allow easy integration with existing projects. The KenHaggerty. Com. SingleUser NuGet package segregates the user logic. SQL scripts to create the schema, tables, and default data are included in the package. Details, screenshots, and related articles can be found at ASP.NET Core 5.0 - Homegrown Analytics Project.
- ASP.NET Core 5.0 - Homegrown Analytics
- ASP.NET Core 5.0 - Analytics Schema and Integration
- ASP.NET Core 5.0 - Cookie Consent and GDPR
- ASP.NET Core 5.0 - Analytic Data Collection
- ASP.NET Core 5.0 - Not Found Errors
- ASP.NET Core 5.0 - Preferred UserAgents
- ASP.NET Core 5.0 - Analytic Dashboard
- ASP.NET Core 5.0 - Online User Count with SignalR
- ASP.NET Core 5.0 - Is Human with Google reCAPTCHA
- ASP.NET Core 5.0 - Log Maintenance with Hangfire
I have upgraded the PageHit entity's UserAgent property from a string to an int. The UserAgentId relates to a child UserAgent entity with a Preferred property.
Entities > UserAgent.cs:
public class UserAgent { [Key] public int Id { get; set; } [Required] [Display(Name = "UserAgent")] [StringLength(512)] public string UAString { get; set; } public bool Preferred { get; set; } public DateTimeOffset CreateDate { get; set; } }
The AnalyticsMiddleware and AnalyticsServices creates a new UserAgent if it does not exist. The initial migration and CreateAnalyticsSchema SQL scripts create a default UserAgent with the primary Id property set to 1. If the AnalyticsMiddleware encounters an issue, the default UserAgent is employed.
I developed a UserAgents page which displays the UserAgent's PageHits count and provides the functions to add or remove the preferred status.
Rather than trying to detect robot page hits, I can filter on Preferred UserAgents.
Comments(0)