locked
How to get Client IP Address on Server side via ConnectionId RRS feed

  • Question

  • User1338262655 posted

    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 ?

    Wednesday, June 5, 2019 10:26 AM

Answers

  • User61956409 posted

    Hi Alter,

    Alter

    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;

    Alter

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 6, 2019 2:29 AM

All replies

  • User475983607 posted

    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.

    Wednesday, June 5, 2019 10:57 AM
  • User753101303 posted

    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

    Wednesday, June 5, 2019 11:04 AM
  • User61956409 posted

    Hi Alter,

    Alter

    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;

    Alter

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 6, 2019 2:29 AM