[英]Log IP of signed in user with ASP.net core Identity MVC
Save the IP of a User hitting specific actions/controllers to my database. 保存用户的IP,将特定操作/控制器命中到我的数据库。 Also since this process takes a significant amount of time it would be good if it gets executed on a background thread or similar.
此外,由于此过程需要花费大量时间,因此如果在后台线程或类似操作上执行它将会很好。
Creating a CustomAuthorizeAttribute that looks something like this: 创建一个如下所示的CustomAuthorizeAttribute:
public class LoggedAuthorizeAttribute : TypeFilterAttribute
{
public LoggedAuthorizeAttribute() : base(typeof(LoggedAuthorizeFilter))
{
}
}
public class LoggedAuthorizeFilter : IAuthorizationFilter
{
private readonly UserManager<User> _userManager;
public LoggedAuthorizeFilter(UserManager<User> userManager)
{
_userManager = userManager;
}
public async void OnAuthorization(AuthorizationFilterContext context)
{
if (!context.HttpContext.User.Identity.IsAuthenticated)
return;
var user = await _userManager.GetUserAsync(context.HttpContext.User);
var remoteIpAddress = context.HttpContext.Connection.RemoteIpAddress;
user.UserLogins.Add(new UserLogin
{LoggedInOn = DateTimeOffset.UtcNow, LoggedInFrom = remoteIpAddress});
await _userManager.UpdateAsync(user);
}
}
Problems with this solution: 此解决方案的问题:
InvalidOperationException
telling A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.
InvalidOperationException
告知A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.
A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.
. Any kind of help is appreciated. 任何形式的帮助表示赞赏。
As suggested by Kirk Larkin
implementing the IAsyncActionFilter instead fixes my second issue I ran into. 正如
Kirk Larkin
所建议实现IAsyncActionFilter而修复了我遇到的第二个问题。 But still how would I perform this in a Background Thread or similar, if this is even the right choice. 但是,如果这是一个正确的选择,我将如何在后台线程或类似的东西中执行此操作。
So I fixed my first issue with the use of a ConcurrentQueue
which dequeues its items on a Background Thread. 所以我修复了我的第一个问题,使用了
ConcurrentQueue
,它在后台线程上将其项目出列。 I found this solution in this blog post. 我在这篇博文中找到了这个解决方案。 It just needs some slight modifications in order to work for this problem.
它只需要稍微修改一下就可以解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.