I need to know the client's IP in order to know his country and set the language accordingly. I already have a client ID and I send him a message like this:
This is a good solution when all Clients are in the same country and understand the same language. But if they are all in different countries - I need to localize the TEXT. And to do it I need to know ip-address from ConnectionId. If getting ip from
Connection Id directly impossible, how to get ip from Context on Hub ?
The browser sends a header that has the users configured culture which is more reliable than an IP address. You should be able to grab the user's culture when the user first requests the SignalR Application. Then put the culture in a Cookie or add as a
constant to the JavaScript application.
For example if I'm travelling abroad it would still use my browser preferred language rather than the language for the country in which I am and that I may not understand at all.
This is a good solution when all Clients are in the same country and understand the same language. But if they are all in different countries - I need to localize the TEXT.
As others suggested, you can try to get client language preferences and
localize the message(s) based on language preference rather than IP address. And if you are using Microsoft.AspNet.SignalR, you can refer to the following code snippet to get user languages.
If getting ip from Connection Id directly impossible, how to get ip from Context on Hub ?
If you indeed need to get IP address from Hub to achieve your requirement, you can try following code snippet.
Context.Request.Environment.TryGetValue("server.RemoteIpAddress", out tempObject);
if (tempObject != null)
{
ipAddress = (string)tempObject;
}
else
{
ipAddress = "";
}
With Regards,
Fei Han
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
2 Points
8 Posts
How to get Client IP Address on Server side via ConnectionId
Jun 05, 2019 10:26 AM|Alter|LINK
I need to know the client's IP in order to know his country and set the language accordingly. I already have a client ID and I send him a message like this:
Clients.Clients (ConnectionIds).SendNotify (TEXT);
This is a good solution when all Clients are in the same country and understand the same language. But if they are all in different countries - I need to localize the TEXT. And to do it I need to know ip-address from ConnectionId. If getting ip from Connection Id directly impossible, how to get ip from Context on Hub ?
All-Star
52971 Points
23566 Posts
Re: How to get Client IP Address on Server side via ConnectionId
Jun 05, 2019 10:57 AM|mgebhard|LINK
The browser sends a header that has the users configured culture which is more reliable than an IP address. You should be able to grab the user's culture when the user first requests the SignalR Application. Then put the culture in a Cookie or add as a constant to the JavaScript application.
All-Star
48490 Points
18068 Posts
Re: How to get Client IP Address on Server side via ConnectionId
Jun 05, 2019 11:04 AM|PatriceSc|LINK
Hi,
Depending on your audience you could consider using https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.userlanguages?view=netframework-4.8 instead as a default and have a way for them to change that default if needed.
For example if I'm travelling abroad it would still use my browser preferred language rather than the language for the country in which I am and that I may not understand at all.
At this step even if not using yet ASP.NET Core it is IMO always interesting to see how it is done in this version. For example see :
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2#implement-a-strategy-to-select-the-languageculture-for-each-request
All-Star
40565 Points
6233 Posts
Microsoft
Re: How to get Client IP Address on Server side via ConnectionId
Jun 06, 2019 02:29 AM|Fei Han - MSFT|LINK
Hi Alter,
As others suggested, you can try to get client language preferences and localize the message(s) based on language preference rather than IP address. And if you are using Microsoft.AspNet.SignalR, you can refer to the following code snippet to get user languages.
If you indeed need to get IP address from Hub to achieve your requirement, you can try following code snippet.
With Regards,
Fei Han