Skip to content

feat(WebClientServer): add UserName property#7601

Merged
ArgoZhang merged 5 commits intomainfrom
feat-user-name
Jan 30, 2026
Merged

feat(WebClientServer): add UserName property#7601
ArgoZhang merged 5 commits intomainfrom
feat-user-name

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Jan 30, 2026

Link issues

fixes #7600

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Associate connected clients with the currently authenticated user and expose their username in connection tracking.

New Features:

  • Add UserName property to ClientInfo to record the logged-in user associated with a web client connection.

Enhancements:

  • Populate ClientInfo.UserName in ConnectionHub using the AuthenticationStateProvider when the user is authenticated.

Tests:

  • Extend ConnectionHub unit tests with a mock AuthenticationStateProvider to verify that ClientInfo.UserName is set for authenticated users.

Copilot AI review requested due to automatic review settings January 30, 2026 03:42
@bb-auto bb-auto Bot added the enhancement New feature or request label Jan 30, 2026
@bb-auto bb-auto Bot added this to the v10.2.0 milestone Jan 30, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jan 30, 2026

Reviewer's Guide

Adds propagation of the currently authenticated user’s name into ConnectionHub’s ClientInfo via AuthenticationStateProvider, and covers it with a unit test plus a new UserName property on ClientInfo.

Sequence diagram for propagating authenticated user name into ClientInfo

sequenceDiagram
  actor Browser
  participant ConnectionHub
  participant AuthenticationStateProvider
  participant ConnectionService

  Browser->>ConnectionHub: Register connection
  ConnectionHub->>AuthenticationStateProvider: GetAuthenticationStateAsync()
  AuthenticationStateProvider-->>ConnectionHub: AuthenticationState with User.Identity
  ConnectionHub->>ConnectionHub: Build ClientInfo with Ip, City, Engine
  ConnectionHub->>ConnectionHub: Set client.UserName if identity.IsAuthenticated
  ConnectionHub->>ConnectionService: AddOrUpdate(client)
Loading

Updated class diagram for ConnectionHub and ClientInfo

classDiagram

class ConnectionHub {
  -IOptions~BootstrapBlazorOptions~ BootstrapBlazorOptions
  -AuthenticationStateProvider AuthenticationStateProvider
  -IIpLocatorProvider _ipLocatorProvider
  -ThrottleOptions _throttleOptions
}

class ClientInfo {
  string Ip
  string City
  string Engine
  string UserName
}

class ConnectionService {
  +AddOrUpdate(client ClientInfo)
}

ConnectionHub --> ClientInfo : populates
ConnectionHub --> ConnectionService : uses
Loading

File-Level Changes

Change Details Files
Inject authentication state into ConnectionHub and populate ClientInfo with the current user name when authenticated.
  • Inject AuthenticationStateProvider into ConnectionHub via [Inject] property.
  • On client callback, retrieve the current AuthenticationState, check for an authenticated identity, and set client.UserName from identity.Name before updating the ConnectionService.
src/BootstrapBlazor/Components/ConnectionHub/ConnectionHub.cs
Extend ClientInfo to carry the logged-in user name.
  • Add a nullable UserName property to ClientInfo with XML documentation in both Chinese and English.
src/BootstrapBlazor/Services/WebClientService.cs
Update ConnectionHub unit test to verify user name propagation using a mock AuthenticationStateProvider.
  • Register a scoped MockAuthenticationStateProvider as the AuthenticationStateProvider in the test DI container.
  • After the beat interval, set the mock provider to an authorized user and invoke Callback again.
  • Assert that the stored ClientInfo.UserName matches the mock user name.
  • Introduce MockAuthenticationStateProvider helper class that can switch to an authenticated ClaimsPrincipal with a specified name.
test/UnitTest/Services/ConnectionHubTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7600 Add a UserName property to the Web client connection information model so it can store the currently logged-in user account.
#7600 Populate the UserName property for each connection using the authenticated user from the AuthenticationStateProvider within ConnectionHub.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Injecting AuthenticationStateProvider directly into ConnectionHub makes the component depend on the auth subsystem being registered; consider falling back gracefully (e.g., making the dependency optional or using TryGetService) so apps without authentication don’t fail at startup.
  • In ConnectionHub.Callback, the authentication state is fetched on every callback; if callbacks are frequent, consider caching the username for the connection or otherwise minimizing repeated GetAuthenticationStateAsync calls to reduce overhead.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Injecting `AuthenticationStateProvider` directly into `ConnectionHub` makes the component depend on the auth subsystem being registered; consider falling back gracefully (e.g., making the dependency optional or using `TryGetService`) so apps without authentication don’t fail at startup.
- In `ConnectionHub.Callback`, the authentication state is fetched on every callback; if callbacks are frequent, consider caching the username for the connection or otherwise minimizing repeated `GetAuthenticationStateAsync` calls to reduce overhead.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Components/ConnectionHub/ConnectionHub.cs:89-91` </location>
<code_context>
                 }
+
+                var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+                var identity = state.User.Identity;
+                if (identity is { IsAuthenticated: true })
+                {
+                    client.UserName = identity.Name;
+                }
</code_context>

<issue_to_address>
**question (bug_risk):** Clarify behavior when a previously authenticated user becomes anonymous to avoid stale usernames.

Right now `client.UserName` is only set when `identity is { IsAuthenticated: true }`. If the user later becomes unauthenticated (logout, timeout), the previous username will remain, so an anonymous client may still appear as a named user.

If that’s undesirable, clear `client.UserName` in the `else` branch when the identity is not authenticated. If you do want to preserve the last known user, double-check that all consumers of `ClientInfo` are prepared for that behavior.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/BootstrapBlazor/Components/ConnectionHub/ConnectionHub.cs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a UserName property to the ClientInfo class that automatically captures the currently logged-in user's name from the authentication state. This enhancement allows tracking which user is associated with each client connection in the ConnectionHub system.

Changes:

  • Added UserName property to ClientInfo class with bilingual XML documentation
  • Modified ConnectionHub component to populate UserName from AuthenticationStateProvider
  • Added test coverage for the authenticated user scenario with a mock authentication state provider

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/BootstrapBlazor/Services/WebClientService.cs Added nullable UserName string property to ClientInfo class
src/BootstrapBlazor/Components/ConnectionHub/ConnectionHub.cs Injected AuthenticationStateProvider and added logic to retrieve and set the authenticated user's name in the Callback method
test/UnitTest/Services/ConnectionHubTest.cs Added mock AuthenticationStateProvider class and test assertions to verify UserName is correctly populated when a user is authenticated

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/BootstrapBlazor/Components/ConnectionHub/ConnectionHub.cs
Comment thread src/BootstrapBlazor/Components/ConnectionHub/ConnectionHub.cs
Comment thread test/UnitTest/Services/ConnectionHubTest.cs
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@beee938). Learn more about missing BASE report.
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #7601   +/-   ##
========================================
  Coverage        ?   100.00%           
========================================
  Files           ?       749           
  Lines           ?     33001           
  Branches        ?      4580           
========================================
  Hits            ?     33001           
  Misses          ?         0           
  Partials        ?         0           
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ArgoZhang ArgoZhang merged commit 88bed84 into main Jan 30, 2026
5 checks passed
@ArgoZhang ArgoZhang deleted the feat-user-name branch January 30, 2026 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(WebClientServer): add UserName property

2 participants