简体   繁体   English

使用ASP.net核心Identity MVC登录用户的日志IP

[英]Log IP of signed in user with ASP.net core Identity MVC

What I want to achieve: 我想要实现的目标:

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. 此外,由于此过程需要花费大量时间,因此如果在后台线程或类似操作上执行它将会很好。

What I tried so far: 到目前为止我尝试了什么:

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: 此解决方案的问题:

  1. When the request hits an action marked with this attribute the request will take about 1-2 seconds until it actually processes the action. 当请求命中标记有此属性的操作时,请求将花费大约1-2秒直到它实际处理该操作。
  2. The UserManager is retrieved by Dependency Injection, but I also access the UserManager instance in some of my Actions which causes a 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. UserManager由依赖注入检索,但我也在我的一些操作中访问UserManager实例,这导致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. 任何形式的帮助表示赞赏。

Update 更新

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM