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 Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.JSInterop;
using System;
using System.Net;
@ -28,6 +30,12 @@ namespace Bootstrap.Admin.Pages.Components
[Inject]
public NavigationManager? NavigationManager { get; set; }
/// <summary>
/// 获得/设置 组件名字
/// </summary>
[Inject]
protected IHttpContextAccessor? HttpContextAccessor { get; set; }
/// <summary>
///
/// </summary>
@ -80,6 +88,12 @@ namespace Bootstrap.Admin.Pages.Components
/// <returns></returns>
protected override async Task OnInitializedAsync()
{
// 网页跳转监控
if (NavigationManager != null)
{
NavigationManager.LocationChanged += NavigationManager_LocationChanged;
}
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
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>

View File

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

View File

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