feat: Blazor 模式下完善访问日志

This commit is contained in:
Argo Zhang 2020-01-30 14:32:48 +08:00
parent 4010fb8efb
commit 8d119f9897
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
3 changed files with 24 additions and 13 deletions

View File

@ -1,9 +1,11 @@
using Bootstrap.Admin.Pages.Extensions; using Bootstrap.Admin.Models;
using Bootstrap.Admin.Models; using Bootstrap.Admin.Pages.Extensions;
using Bootstrap.Admin.Pages.Shared; using Bootstrap.Admin.Pages.Shared;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Server; using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using System; using System;
using System.Net; using System.Net;
@ -28,6 +30,12 @@ namespace Bootstrap.Admin.Pages.Components
[Inject] [Inject]
public NavigationManager? NavigationManager { get; set; } public NavigationManager? NavigationManager { get; set; }
/// <summary>
/// 获得/设置 组件名字
/// </summary>
[Inject]
protected IHttpContextAccessor? HttpContextAccessor { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -80,6 +88,12 @@ namespace Bootstrap.Admin.Pages.Components
/// <returns></returns> /// <returns></returns>
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
// 网页跳转监控
if (NavigationManager != null)
{
NavigationManager.LocationChanged += NavigationManager_LocationChanged;
}
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (!state.User.Identity.IsAuthenticated) if (!state.User.Identity.IsAuthenticated)
{ {
@ -92,6 +106,12 @@ namespace Bootstrap.Admin.Pages.Components
} }
} }
private void NavigationManager_LocationChanged(object? sender, LocationChangedEventArgs e)
{
var name = $"/{NavigationManager?.ToBaseRelativePath(e.Location)}";
if (HttpContextAccessor != null) HttpContextAccessor.HttpContext?.SaveOnlineUser(name);
}
/// <summary> /// <summary>
/// 设置参数方法 /// 设置参数方法
/// </summary> /// </summary>

View File

@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering; using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Http;
using System; using System;
namespace Bootstrap.Admin.Pages.Components namespace Bootstrap.Admin.Pages.Components
@ -10,12 +9,6 @@ namespace Bootstrap.Admin.Pages.Components
/// </summary> /// </summary>
public class PageContent : ComponentBase public class PageContent : ComponentBase
{ {
/// <summary>
/// 获得/设置 组件名字
/// </summary>
[Inject]
protected IHttpContextAccessor? HttpContextAccessor { get; set; }
/// <summary> /// <summary>
/// 获得/设置 组件名字 /// 获得/设置 组件名字
/// </summary> /// </summary>
@ -34,8 +27,6 @@ namespace Bootstrap.Admin.Pages.Components
var t = Type.GetType($"Bootstrap.Admin.Pages.Views.{name}"); var t = Type.GetType($"Bootstrap.Admin.Pages.Views.{name}");
if (t != null) if (t != null)
{ {
// 访问日志
if (HttpContextAccessor != null) HttpContextAccessor.HttpContext?.SaveOnlineUser($"/Pages/{Name}");
builder.OpenComponent(0, t); builder.OpenComponent(0, t);
builder.CloseComponent(); builder.CloseComponent();

View File

@ -1,9 +1,9 @@
using System;
using System.Net;
using Bootstrap.DataAccess; using Bootstrap.DataAccess;
using Longbow.Cache; using Longbow.Cache;
using Longbow.Web; using Longbow.Web;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
namespace Microsoft.AspNetCore.Http namespace Microsoft.AspNetCore.Http
{ {