https://github.com/sql-mistermagoo/blazorservershowipaddress
Interactive Blazor Server demo getting IP Address two ways
https://github.com/sql-mistermagoo/blazorservershowipaddress
Last synced: 9 months ago
JSON representation
Interactive Blazor Server demo getting IP Address two ways
- Host: GitHub
- URL: https://github.com/sql-mistermagoo/blazorservershowipaddress
- Owner: SQL-MisterMagoo
- Created: 2025-02-16T17:47:02.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-16T17:55:16.000Z (over 1 year ago)
- Last Synced: 2025-07-13T01:07:49.383Z (12 months ago)
- Language: CSS
- Size: 725 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Getting Client IP Address in Blazor
This solution demonstrates two ways to get the client IP Address and display it in Blazor components:
1. **Using `IHttpContextAccessor` in `App.razor`**:
- The `IHttpContextAccessor` is injected into the `App.razor` component to access the client's IP address directly from the HTTP context.
- The IP address is then passed to the `Routes` component as a parameter and then shared via a CascadingValue to all children.
2. **Using a Custom `ICircuitHandler` and `AppState` Service**:
- A custom `ICircuitHandler` implementation (`CustomCircuitHandler`) is created to handle circuit connections and disconnections.
- The `CustomCircuitHandler` uses `IHttpContextAccessor` to get the client's IP address and stores it in a scoped `AppState` service along with the circuit ID.
- The `Home.razor` component injects the `AppState` service and retrieves the IP address and circuit ID to display them on the screen.
- This will only work if web sockets are enabled.
[Sample](https://blazorip-bah4f8e7avdpfycs.northeurope-01.azurewebsites.net/) published on Azure Web app (free tier)