!1918 refactor(#I4CQ5G): remove Microsoft.AspNetCore.Http dependence

* doc: 更新客户端信息示例文档
* refactor: 未登录用户赋值 null
* doc: 更新地址位置文档
* chore: add BootstrapBlazor.Middleware dependence
* chore: remove test branch
* chore: update ci script
* feat: 增加 BootstrapBlazor.Middleware 中间件
* refactor: 组件库移除 UseBootstrapBlazor 中间件
This commit is contained in:
Argo 2021-10-05 03:19:09 +00:00
parent 6fe263eed7
commit e0073bd0f4
14 changed files with 106 additions and 48 deletions

View File

@ -44,3 +44,6 @@ jobs:
dotnet pack src/Extensions/DataServices/BootstrapBlazor.DataAcces.FreeSql -c Release -o publish dotnet pack src/Extensions/DataServices/BootstrapBlazor.DataAcces.FreeSql -c Release -o publish
dotnet nuget push publish/BootstrapBlazor.DataAcces.FreeSql.*.nupkg -s https://api.nuget.org/v3/index.json -k $NUGET_API_KEY --skip-duplicate dotnet nuget push publish/BootstrapBlazor.DataAcces.FreeSql.*.nupkg -s https://api.nuget.org/v3/index.json -k $NUGET_API_KEY --skip-duplicate
dotnet pack src/Middleware/BootstrapBlazor.Middleware -c Release -o publish
dotnet nuget push publish/BootstrapBlazor.Middleware.*.nupkg -s https://api.nuget.org/v3/index.json -k $NUGET_API_KEY --skip-duplicate

View File

@ -109,6 +109,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{8AB9DB
NuGet.Config = NuGet.Config NuGet.Config = NuGet.Config
EndProjectSection EndProjectSection
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Middleware", "Middleware", "{4A7F4513-C515-40DD-A217-169E1CBB2827}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.Middleware", "src\Middleware\BootstrapBlazor.Middleware\BootstrapBlazor.Middleware.csproj", "{59B49D4C-05FB-4E51-AB40-9498ADC71DD0}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -163,6 +167,10 @@ Global
{FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5}.Debug|Any CPU.Build.0 = Debug|Any CPU {FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5}.Release|Any CPU.ActiveCfg = Release|Any CPU {FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5}.Release|Any CPU.Build.0 = Release|Any CPU {FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5}.Release|Any CPU.Build.0 = Release|Any CPU
{59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -189,6 +197,8 @@ Global
{1ED371F3-2B28-4B2D-91B8-0C00DA42CB65} = {A2182155-43ED-44C1-BF6F-1B70EBD2DFFE} {1ED371F3-2B28-4B2D-91B8-0C00DA42CB65} = {A2182155-43ED-44C1-BF6F-1B70EBD2DFFE}
{0556D9AB-8673-4248-8817-4D99F4DCC568} = {C8E79F4C-8C55-4E13-96B5-3D2BD6A07B74} {0556D9AB-8673-4248-8817-4D99F4DCC568} = {C8E79F4C-8C55-4E13-96B5-3D2BD6A07B74}
{FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5} = {C8E79F4C-8C55-4E13-96B5-3D2BD6A07B74} {FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5} = {C8E79F4C-8C55-4E13-96B5-3D2BD6A07B74}
{4A7F4513-C515-40DD-A217-169E1CBB2827} = {A2182155-43ED-44C1-BF6F-1B70EBD2DFFE}
{59B49D4C-05FB-4E51-AB40-9498ADC71DD0} = {4A7F4513-C515-40DD-A217-169E1CBB2827}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B} SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B}

View File

@ -5,6 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BootstrapBlazor.Middleware" Version="1.*" />
<PackageReference Include="Longbow.Tasks" Version="5.*" /> <PackageReference Include="Longbow.Tasks" Version="5.*" />
</ItemGroup> </ItemGroup>

View File

@ -2,7 +2,7 @@
<h3>获取客户端连接信息</h3> <h3>获取客户端连接信息</h3>
<h4>多用系统日志跟踪</h4> <h4>多用系统日志跟踪</h4>
<Block Title="普通用法" Introduction="注入服务显示客户端信息"> <Block Title="普通用法" Introduction="注入服务显示客户端信息">
<p>用法介绍</p> <p>用法介绍</p>
@ -21,6 +21,11 @@
}); });
}</Pre> }</Pre>
</div> </div>
<Tips>
<p><code>app.UseBootstrapBlazor</code> 中间件位于程序集 <code>BootstrapBlazor.Middleware</code>,请自行引用此包才能正常使用</p>
</Tips>
<div class="mb-3"> <div class="mb-3">
<p>2. 组件中使用注入服务 <code>WebClientService</code> 调用 <code>RetrieveRemoteInfo</code> 方法</p> <p>2. 组件中使用注入服务 <code>WebClientService</code> 调用 <code>RetrieveRemoteInfo</code> 方法</p>
<Pre>[Inject] <Pre>[Inject]
@ -39,13 +44,14 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
} }
</Pre> </Pre>
</div> </div>
<GroupBox Title="您的连接信息"> <GroupBox Title="您的连接信息">
<div class="row g-3 form-inline"> <div class="row g-3 form-inline">
<div class="col-12 col-sm-6"> <div class="col-12 col-sm-6">
<Display Value="ClientService.Id" DisplayText="连接 ID" ShowLabel="true" /> <Display Value="ClientService.Id" DisplayText="连接 ID" ShowLabel="true" />
</div> </div>
<div class="col-12 col-sm-6"> <div class="col-12 col-sm-6">
<Display Value="ClientService.UserName" DisplayText="当前用户" ShowLabel="true" /> <Display Value="GetUserName()" DisplayText="当前用户" ShowLabel="true" />
</div> </div>
<div class="col-12 col-sm-6"> <div class="col-12 col-sm-6">
<Display Value="ClientService.Ip" DisplayText="IP 地址" ShowLabel="true" /> <Display Value="ClientService.Ip" DisplayText="IP 地址" ShowLabel="true" />

View File

@ -34,5 +34,7 @@ namespace BootstrapBlazor.Shared.Pages
StateHasChanged(); StateHasChanged();
} }
} }
private string GetUserName() => ClientService.UserName ?? "未登录";
} }
} }

View File

@ -2,7 +2,7 @@
<h3>获取 IP 地理位置</h3> <h3>获取 IP 地理位置</h3>
<h4>多用系统日志跟踪</h4> <h4>多用系统日志跟踪与分析</h4>
<Block Title="普通用法" Introduction="注入服务显示客户端地理位置信息"> <Block Title="普通用法" Introduction="注入服务显示客户端地理位置信息">
<p><b>用法介绍</b></p> <p><b>用法介绍</b></p>

View File

@ -38,8 +38,4 @@
<None Include="wwwroot\**\*.*" /> <None Include="wwwroot\**\*.*" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup>
</Project> </Project>

View File

@ -103,7 +103,7 @@ namespace BootstrapBlazor.Components
// UserName // UserName
var state = await _authenticationStateProvider.GetAuthenticationStateAsync(); var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
UserName = (state.User.Identity?.IsAuthenticated ?? false) ? state.User.Identity.Name : ""; UserName = (state.User.Identity?.IsAuthenticated ?? false) ? state.User.Identity.Name : null;
ReturnTask = new TaskCompletionSource<bool>(); ReturnTask = new TaskCompletionSource<bool>();
return await ReturnTask.Task; return await ReturnTask.Task;
} }

View File

@ -4,14 +4,10 @@
using BootstrapBlazor.Components; using BootstrapBlazor.Components;
using BootstrapBlazor.Localization.Json; using BootstrapBlazor.Localization.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Text.Json;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
@ -73,40 +69,5 @@ namespace Microsoft.Extensions.DependencyInjection
}); });
return services; return services;
} }
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseBootstrapBlazor(this IApplicationBuilder builder)
{
// 获得客户端 IP 地址
builder.UseWhen(context => context.Request.Path.StartsWithSegments("/ip.axd"), app => app.Run(async context =>
{
var ip = "";
var headers = context.Request.Headers;
if (headers.ContainsKey("X-Forwarded-For"))
{
var ips = new List<string>();
foreach (var xf in headers["X-Forwarded-For"])
{
if (!string.IsNullOrEmpty(xf))
{
ips.Add(xf);
}
}
ip = string.Join(";", ips);
}
else
{
ip = context.Connection.RemoteIpAddress.ToIPv4String();
}
context.Response.Headers.Add("Content-Type", new Microsoft.Extensions.Primitives.StringValues("application/json; charset=utf-8"));
await context.Response.WriteAsync(JsonSerializer.Serialize(new { Id = context.TraceIdentifier, Ip = ip }));
}));
return builder;
}
} }
} }

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,51 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Text.Json;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
///
/// </summary>
public static class BootstrapBlazorApplicationBuilderExtensions
{
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseBootstrapBlazor(this IApplicationBuilder builder)
{
// 获得客户端 IP 地址
builder.UseWhen(context => context.Request.Path.StartsWithSegments("/ip.axd"), app => app.Run(async context =>
{
var ip = "";
var headers = context.Request.Headers;
if (headers.ContainsKey("X-Forwarded-For"))
{
var ips = new List<string>();
foreach (var xf in headers["X-Forwarded-For"])
{
if (!string.IsNullOrEmpty(xf))
{
ips.Add(xf);
}
}
ip = string.Join(";", ips);
}
else
{
ip = context.Connection.RemoteIpAddress.ToIPv4String();
}
context.Response.Headers.Add("Content-Type", new Microsoft.Extensions.Primitives.StringValues("application/json; charset=utf-8"));
await context.Response.WriteAsync(JsonSerializer.Serialize(new { Id = context.TraceIdentifier, Ip = ip }));
}));
return builder;
}
}
}

View File

@ -0,0 +1,19 @@
<Project>
<Import Project="..\..\Directory.Build.props" />
<PropertyGroup>
<IsPackable>true</IsPackable>
<PackageIcon>logo.png</PackageIcon>
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>
<ItemGroup>
<None Include="logo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project>

View File

@ -1,8 +1,10 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. // Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using System.Net; using System.Net;
namespace BootstrapBlazor.Components namespace Microsoft.AspNetCore.Builder
{ {
/// <summary> /// <summary>
/// IPAddress 内部操作扩展类 /// IPAddress 内部操作扩展类

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB