49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
|
using Bootstrap.Admin;
|
|||
|
using Microsoft.AspNetCore.Http;
|
|||
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
using System;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Microsoft.AspNetCore.Builder
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public static class OnlineUsersMiddlewareExtensions
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="builder"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static IApplicationBuilder UseOnlineUsers(this IApplicationBuilder builder) => builder.UseWhen(context => context.Filter(), app => app.Use(async (context, next) =>
|
|||
|
{
|
|||
|
await Task.Run(() =>
|
|||
|
{
|
|||
|
var onlineUsers = context.RequestServices.GetService<IOnlineUsers>();
|
|||
|
var clientIp = context.Connection.RemoteIpAddress.ToString();
|
|||
|
onlineUsers.AddOrUpdate(clientIp, key =>
|
|||
|
{
|
|||
|
var ou = new OnlineUser(key, context.User.Identity.Name, context.Request.Method);
|
|||
|
ou.AddRequestUrl(context.Request.Path);
|
|||
|
return ou;
|
|||
|
}, (key, v) =>
|
|||
|
{
|
|||
|
v.LastAccessTime = DateTime.Now;
|
|||
|
v.Method = context.Request.Method;
|
|||
|
v.AddRequestUrl(context.Request.Path);
|
|||
|
return v;
|
|||
|
});
|
|||
|
});
|
|||
|
await next();
|
|||
|
}));
|
|||
|
|
|||
|
private static bool Filter(this HttpContext context)
|
|||
|
{
|
|||
|
var url = context.Request.Path;
|
|||
|
return !new string[] { "/api", "/NotiHub", "/swagger" }.Any(r => url.StartsWithSegments(r, StringComparison.OrdinalIgnoreCase));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|