I'm trying to access IHttpContextAccessor And IUserRetrieve services within my Events.OnSignedIn method in my Configure Services.
The below line of code worked in an older version of .net core however, in .NET CORE 5.0 this no longer works.
Any Suggestions?
o.Events.OnSignedIn = async ctx =>
{
var claims = ctx.Principal;
var user = Dependency.Resolve<IUserRetrieveService>().ByUsername(ctx.Principal.Identity.Name) as UserDefinition;
var remoteIpAddress = Dependency.Resolve<IHttpContextAccessor>().HttpContext.Connection.RemoteIpAddress;
if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress).AddressList
.First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
}
};
Reference: Services injected into Startup
Note that CookieSignedInContext provides access to the current request's
HttpContext
which can be used to resolve request services as needed.