How to get user Browser name ( user-agent ) in Asp.net Core?
I think this was an easy one. Got the answer in Request.Headers["User-Agent"].ToString()
How to get browser information of client?
You can install Wangkanai.Detection
package. The full documentation could be found here: https://github.com/wangkanai/Detection
Installation of detection library is now done with a single package
reference point.
PM> install-package Wangkanai.Detection -pre
While it is still possible to install the individual package if you just need that specific resolver.
PM> install-package Wangkanai.Detection.Device -pre
PM> install-package Wangkanai.Detection.Browser -pre
PM> install-package Wangkanai.Detection.Engine -pre //concept
PM> install-package Wangkanai.Detection.Platform -pre //concept
PM> install-package Wangkanai.Detection.Crawler -pre
Installation of Responsive
library will bring in all dependency packages (This will include Wangkanai.Detection.Device
).
PM> install-package Wangkanai.Responsive -pre
I think the following should be enough for you:
install-package Wangkanai.Detection -pre
install-package Wangkanai.Detection.Browser -pre
Then you need to configure the Startup.cs
by adding the detection service in the ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
// Add detection services container and device resolver service.
services.AddDetection();
services.AddDetectionCore().AddBrowser();
// Add framework services.
services.AddMvc();
}
And finally in your Controller
, do something like this:
public class HomeController : Controller
{
private readonly IDetection _detection;
public HomeController(IDetection detection)
{
_detection = detection;
}
public IActionResult Index()
{
string browser_information = _detection.Browser.Type.ToString() +
_detection.Browser.Version;
//...
}
}
Trying to get the user-agent from request in asp.net web api self host
The absolutely simplest way to get the full user-agent from inside a WebAPI-controller is by doing this:
var userAgent = Request.Headers.UserAgent.ToString();
It gives exactly the same result as doing the manual step like this:
// var headers = request.Headers.GetValues("User-Agent");
// var userAgent = string.Join(" ", headers);
How to check user-agent in ASP.NET Core health check calls (MapHealthChecks)?
You can create a policy which performs user agent requirement validation
public class UserAgentRequirement : IAuthorizationRequirement
{
public string UserAgent { get; }
public UserAgentRequirement(string userAgent)
{
UserAgent = userAgent;
}
}
public class UserAgentAuthorizationHandler : AuthorizationHandler<UserAgentRequirement>
{
private readonly IHttpContextAccessor httpContextAccessor;
public UserAgentAuthorizationHandler(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserAgentRequirement requirement)
{
var httpContext = httpContextAccessor.HttpContext;
var agent = httpContext.Request.Headers["User-Agent"];
if (agent == requirement.UserAgent)
{
context.Succeed(requirement);
}
else
{
context.Fail();
}
return Task.CompletedTask;
}
}
Do not forget to register IHttpContextAccessor
and UserAgentAuthorizationHandler
. In Startup.cs
services.AddHttpContextAccessor();
services.AddScoped<IAuthorizationHandler, UserAgentAuthorizationHandler>();
services.AddAuthorization(options =>
{
//...
options.AddPolicy("HealthCheckPolicy", builder =>
{
builder.AddRequirements(new UserAgentRequirement("ReadyForRequest/1.0"));
});
});
//...
app.UseEndpoints(endpoints =>
{
endpoints
.MapHealthChecks("/health", new HealthCheckOptions { AllowCachingResponses = false })
.RequireAuthorization("HealthCheckPolicy");
//...
});
Related Topics
What Is the Correct Way to Compare Char Ignoring Case
Check If Datetime Is a Weekend or a Weekday
System.Collections.Generic.List Does Not Contain a Definition for 'Select'
How to Set a Cookie on Httpclient'S Httprequestmessage
How to Authenticate a Webclient Request
Httpclient Post With Parameters in Body
How to Make Blazor Http Get Json Async Request
How to Save Mailmessage Object to Disk as *.Eml or *.Msg File
Filling a Character Array With Characters from a String
Programmatically Create and Launch and Rdp Session (Without Gui)
How to Get Group of Records With Latest Date Using Linq to Entities
Trying to Add Spaces Between Characters in a String in C#
Load Image from Resources Area of Project in C#
Getting the Latest File Modified from Azure Blob
Itextsharp Pdfptable How to Make a Border Around Entire Table
Select Multiple Fields from List in Linq
C# Double - Tostring() Formatting With Two Decimal Places But No Rounding