I'm working with ASP.NET Boilerplate Core and Angular.
On user-login, I need to identify the users workstation in some way. IP as the Identifier would be best. Since I use my application only in Intranet, the hostname might be sufficient too. Best would be to keep this information in the session object.
Any ideas?
Thanks for the much appreciated help!
You cannot reach Request object from application services. You can only grab it from Controllers. What you can do is creating a new class which implements IAuditInfoProvider and register the class to dependency injection. (With IsDefault is set to True).
Component.For<IAuditInfoProvider>().ImplementedBy<WebAuditInfoProvider>().IsDefault();
Extend AuditInfo class and add the client name field
public string ClientName { get; set; }
The new class inherited from IAuditInfoProvider There's a method to get the client computer name.
public class WebAuditInfoProvider : IAuditInfoProvider, ITransientDependency
{
public ILogger Logger { get; set; }
private readonly HttpContext _httpContext;
/// <summary>
/// Creates a new <see cref="WebAuditInfoProvider"/>.
/// </summary>
public WebAuditInfoProvider()
{
_httpContext = HttpContext.Current;
Logger = NullLogger.Instance;
}
public void Fill(AuditInfo auditInfo)
{
var httpContext = HttpContext.Current ?? _httpContext;
if (httpContext == null)
{
return;
}
try
{
auditInfo.ClientName = GetComputerName(httpContext);
}
catch (Exception ex)
{
Logger.Warn("Could not obtain web parameters for audit info.");
Logger.Warn(ex.ToString(), ex);
}
}
private static string GetComputerName(HttpContext httpContext)
{
if (!httpContext.Request.IsLocal)
{
return null;
}
try
{
var clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ??
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
return Dns.GetHostEntry(IPAddress.Parse(clientIp)).HostName;
}
catch
{
return null;
}
}
}
I have not tested all these codes but it's a good guide to show you the starting points of the solution.
Also you can take a look the below issue... https://github.com/aspnetboilerplate/aspnetboilerplate/issues/467