-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHttpUserAgentParserAccessor.cs
38 lines (32 loc) · 1.27 KB
/
HttpUserAgentParserAccessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright © https://myCSharp.de - all rights reserved
using Microsoft.AspNetCore.Http;
using MyCSharp.HttpUserAgentParser.Providers;
namespace MyCSharp.HttpUserAgentParser.AspNetCore;
/// <summary>
/// User Agent parser accessor. Implements <see cref="IHttpContextAccessor.HttpContext"/>
/// </summary>
/// <remarks>
/// Creates a new instance of <see cref="HttpUserAgentParserAccessor"/>
/// </remarks>
public class HttpUserAgentParserAccessor(IHttpUserAgentParserProvider httpUserAgentParser)
: IHttpUserAgentParserAccessor
{
private readonly IHttpUserAgentParserProvider _httpUserAgentParser = httpUserAgentParser;
/// <summary>
/// User agent of current <see cref="IHttpContextAccessor"/>
/// </summary>
public string? GetHttpContextUserAgent(HttpContext httpContext)
=> httpContext.GetUserAgentString();
/// <summary>
/// Returns current <see cref="HttpUserAgentInformation"/> of current <see cref="IHttpContextAccessor"/>
/// </summary>
public HttpUserAgentInformation? Get(HttpContext httpContext)
{
string? httpUserAgent = GetHttpContextUserAgent(httpContext);
if (string.IsNullOrEmpty(httpUserAgent))
{
return null;
}
return _httpUserAgentParser.Parse(httpUserAgent);
}
}