如何在Blazor .NET 6(WebAssembly)中获取客户端IP。

huangapple go评论94阅读模式
英文:

how to get client ip with blazor .net 6 (webassembly)

问题

"我正在尝试获取访问页面的用户的IP地址,但我无法发起请求来获取IP,因为我不知道如何解决这个'cors'问题。

来自' http://localhost:5200' 的对'https://api.ipify.org/?format=json&callback=JSONP_CALLBACK' 的跨源请求已被CORS策略阻止:对预检请求的响应未通过访问控制检查:所请求资源上没有'Access-Control-Allow-Origin'标头。如果不透明的响应满足您的需求,请将请求的模式设置为'no-cors',以禁用CORS获取资源。
"

"我尝试添加addCors,但在WebAssembly应用程序中它不存在。"

英文:

I'm trying to get the ip of the user who is accessing the page, but I can't make a request to get the ip, because I don't know how to solve this 'cors' problem.

Access to fetch at 'https://api.ipify.org/?format=json&callback=JSONP_CALLBACK' from origin 'http://localhost:5200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

如何在Blazor .NET 6(WebAssembly)中获取客户端IP。

I tried to add addCors, but it doesn't exist in a webassembly application

答案1

得分: 2

无论如何,当您想获取客户端IP时,客户端应用程序(Blazor Webassembly)将返回IP地址为127.0.0.1。我使用以下代码来测试它:

<script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
<script>
    window.getip = () => {
        console.log(returnCitySN["cip"]);
    };
</script>

您可以看到测试的结果,返回了127.0.0.1。

至于您的问题,您在Blazor Webassembly应用程序中无法做太多来解决CORS问题。从您的截图中,我可以看到您想要调用API。您可以尝试在API网站中设置CORS规则。

此外,您还可以尝试使用服务器端应用程序,如Blazor Server,它可以实现您的目标。而且,如果您选择不使用服务器端应用程序,我找到了这行代码,也可以实现相同的效果:

<script language="JavaScript" src="https://www.hashemian.com/js/visitorIP.js.php"></script>

这是它的测试结果。

英文:

No matter how to do it, when you want to get the client ip, a client side app(Blazor Webassembly) will return the ip as 127.0.0.1. I used the following codes to test it:

&lt;script src=&quot;https://pv.sohu.com/cityjson?ie=utf-8&quot;&gt;&lt;/script&gt;
&lt;script&gt;
&#160;&#160;&#160; window.getip = () =&gt; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; console.log(returnCitySN[&quot;cip&quot;]);
&#160;&#160;&#160; };
&lt;/script&gt;

You can see the result of test, which returned the 127.0.0.1:
如何在Blazor .NET 6(WebAssembly)中获取客户端IP。

Back to your issue here, you can do little in blazor webassembly app to solve the CORS issue. From your screenshot, I can see you want to call the api. You can try to set the CORS rules in the api website.

Also, you can try to use the server-side app like blazor server, which can achieve your goal. What's more, if you choose not to use the server-side app, I found this line of code which can also do that:

&lt;script language=&quot;JavaScript&quot; src=&quot;https://www.hashemian.com/js/visitorIP.js.php&quot;&gt;&lt;/script&gt;

Here is the test result of it:
如何在Blazor .NET 6(WebAssembly)中获取客户端IP。

huangapple
  • 本文由 发表于 2023年3月21日 00:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定