Skip to content

Commit 582eebd

Browse files
authored
feat(IBrowserFingerService): add GetClientHubIdAsync method (#6551)
* feat(IBrowserFingerService): add GetClientHubIdAsync method * feat: 增加 getClientHubId 导出方法 * doc: 更新示例 * doc: 调整间隙 * refactor: 更新脚本降低对浏览器的要求 * doc: 文本框更改为只读 * test: 增加单元测试
1 parent cc70a2c commit 582eebd

File tree

9 files changed

+48
-7
lines changed

9 files changed

+48
-7
lines changed

src/BootstrapBlazor.Server/Components/Samples/BrowserFingers.razor

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ private async Task GetFingerCodeAsync()
1212
}</Pre>
1313
<BootstrapInputGroup>
1414
<BootstrapInputGroupLabel DisplayText="Browser-Finger"></BootstrapInputGroupLabel>
15-
<BootstrapInput @bind-Value="@_code" />
15+
<BootstrapInput Value="@_code" Readonly="true" />
16+
</BootstrapInputGroup>
17+
18+
<BootstrapInputGroup class="mt-3">
19+
<BootstrapInputGroupLabel DisplayText="ClientHubId"></BootstrapInputGroupLabel>
20+
<BootstrapInput Value="@_clientHubId" Readonly="true" />
1621
</BootstrapInputGroup>
1722
</DemoBlock>
1823

src/BootstrapBlazor.Server/Components/Samples/BrowserFingers.razor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public partial class BrowserFingers
1515
private IBrowserFingerService? BrowserFingerService { get; set; }
1616

1717
private string? _code;
18+
private string? _clientHubId;
1819

1920
/// <summary>
2021
/// <inheritdoc/>
@@ -25,10 +26,13 @@ protected override async Task OnInitializedAsync()
2526
await base.OnInitializedAsync();
2627

2728
_code = await GetFingerCodeAsync();
29+
_clientHubId = await GetClientHubIdAsync();
2830
}
2931

3032
private Task<string?> GetFingerCodeAsync() => BrowserFingerService.GetFingerCodeAsync();
3133

34+
private Task<string?> GetClientHubIdAsync() => BrowserFingerService.GetClientHubIdAsync();
35+
3236
private MethodItem[] GetMethods() =>
3337
[
3438
new()
@@ -37,6 +41,13 @@ private MethodItem[] GetMethods() =>
3741
Description = Localizer["GetFingerCodeAsync"],
3842
Parameters = " — ",
3943
ReturnValue = "Task<string?>"
44+
},
45+
new()
46+
{
47+
Name = "GetClientHubIdAsync",
48+
Description = Localizer["GetClientHubIdAsync"],
49+
Parameters = " — ",
50+
ReturnValue = "Task<string?>"
4051
}
4152
];
4253
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6687,7 +6687,8 @@
66876687
"BootstrapBlazor.Server.Components.Samples.BrowserFingers": {
66886688
"BrowserFingerTitle": "Browser fingerprint",
66896689
"BrowserFingerIntro": "Obtain the client browser fingerprint by calling the <code>IBrowserFingerService</code> service instance method <code>GetFingerCodeAsync</code>. The fingerprint is consistent in privacy mode",
6690-
"GetFingerCodeAsync": "Method for obtaining fingerprints"
6690+
"GetFingerCodeAsync": "Method for obtaining fingerprints",
6691+
"GetClientHubIdAsync": "Method for obtaining client hub Id"
66916692
},
66926693
"BootstrapBlazor.Server.Components.Samples.SvgEditors": {
66936694
"SvgEditorTitle": "SvgEditor",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6687,7 +6687,8 @@
66876687
"BootstrapBlazor.Server.Components.Samples.BrowserFingers": {
66886688
"BrowserFingerTitle": "浏览器指纹",
66896689
"BrowserFingerIntro": "通过调用 <code>IBrowserFingerService</code> 服务实例方法 <code>GetFingerCodeAsync</code> 获得客户端浏览器指纹,隐私模式下指纹是一致的",
6690-
"GetFingerCodeAsync": "获得指纹方法"
6690+
"GetFingerCodeAsync": "获得指纹方法",
6691+
"GetClientHubIdAsync": "获得客户端连接 Id 方法"
66916692
},
66926693
"BootstrapBlazor.Server.Components.Samples.SvgEditors": {
66936694
"SvgEditorTitle": "Svg 编辑器",

src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ class DefaultBrowserFingerService(IJSRuntime jSRuntime) : IBrowserFingerService
1313
[NotNull]
1414
private JSModule? _module = null;
1515

16-
/// <summary>
17-
/// 获取剪切板数据方法
18-
/// </summary>
1916
public async Task<string?> GetFingerCodeAsync(CancellationToken token = default)
2017
{
2118
_module ??= await jSRuntime.LoadUtility();
2219
return await _module.InvokeAsync<string?>("getFingerCode", token);
2320
}
21+
22+
public async Task<string?> GetClientHubIdAsync(CancellationToken token = default)
23+
{
24+
_module ??= await jSRuntime.LoadUtility();
25+
return await _module.InvokeAsync<string?>("getClientHubId", token);
26+
}
2427
}

src/BootstrapBlazor/Services/IBrowserFingerService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public interface IBrowserFingerService
1515
/// </summary>
1616
/// <returns></returns>
1717
Task<string?> GetFingerCodeAsync(CancellationToken token = default);
18+
19+
/// <summary>
20+
/// 获得当前连接的客户端 ID 由 BootstrapBlazor 组件框架提供
21+
/// </summary>
22+
/// <param name="token"></param>
23+
/// <returns></returns>
24+
Task<string?> GetClientHubIdAsync(CancellationToken token = default);
1825
}

src/BootstrapBlazor/wwwroot/modules/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ export async function getClientInfo(url) {
2424
if (result) {
2525
data.ip = result.Ip;
2626
}
27-
data.id = localStorage.getItem('bb_hub_connection_id') ?? result.Id;
27+
data.id = localStorage.getItem('bb_hub_connection_id') || result.Id;
2828
return data;
2929
}

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,10 @@ export function getNetworkInfo() {
919919
return null;
920920
}
921921

922+
export function getClientHubId() {
923+
return localStorage.getItem('bb_hub_connection_id');
924+
}
925+
922926
export {
923927
autoAdd,
924928
autoRemove,

test/UnitTest/Services/BrowserFingerServiceTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,13 @@ public async Task GetFingerCodeAsync_Ok()
1515
var code = await service.GetFingerCodeAsync();
1616
Assert.Equal("9527", code);
1717
}
18+
19+
[Fact]
20+
public async Task GetClientHubIdAsync_Ok()
21+
{
22+
Context.JSInterop.Setup<string?>("getClientHubId").SetResult("9528");
23+
var service = Context.Services.GetRequiredService<IBrowserFingerService>();
24+
var code = await service.GetClientHubIdAsync();
25+
Assert.Equal("9528", code);
26+
}
1827
}

0 commit comments

Comments
 (0)