Last post Jun 06, 2019 02:29 AM by Fei Han - MSFT
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 ?
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
Jun 05, 2019 11:04 AM|PatriceSc|LINK
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 :
Jun 06, 2019 02:29 AM|Fei Han - MSFT|LINK
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.
String userLang = Context.Request.GetHttpContext().Request.UserLanguages;
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;
ipAddress = "";