How to Get IP Address in Blazor WebAssembly
You can use a third party API, such as https://www.ipv4ipv6.net/api/IpAddress/, to get the IP address of the current request in Blazor. The script https://www.ipv4ipv6.net/api/IpAddress/ will return the IP address in text format. But this is a workaround. As of now, there is no way to achieve this. In the following code snippet, the IP address will be retrieved from the https://www.ipv4ipv6.net/api/IpAddress/ API through a JS interop call to the Blazor server.
[_Host.cshtml/index.html]
<script>
function GetAddress() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://www.ipv4ipv6.net/api/IpAddress/";
document.getElementsByTagName("head")[0].appendChild(script);
};
function DisplayIP(response) {
document.getElementById("text").innerHTML = "Your IP Address is " + response.ip;
}
</script>
[index.razor]
@page "/"
@inject IJSRuntime jsRuntime
<button @onclick="GetIp">Get IP!!!</button>
<textarea id="text" style="width:50%;height:40px;" />
@code{
public async Task GetIp()
{
await jsRuntime.InvokeAsync<object>("GetAddress");
}
}
Ohter
github.com
https://github.com/vandycknick/cloudflare-ddns
Cloudflare DDNS allows you to run your own Dynamic DNS service from the comfort of your own infrastructure. Combined with a Raspberry PI or whatever spare hardware you still have lying around, it helps promote a more decentralized internet at a low cost. During execution, it resolves your current public IPv4 and IPv6 addresses and creates/updates DNS records in Cloudflare to point to your home address. It keeps track of any DNS records that it is managing, giving you some peace of mind and allowing it to run in non-empty Cloudflare zones without having to worry that your public blog gets taken down. Any stale or duplicate records will get safely cleaned up.
Comments
Comments are closed