开发者

Advice needed: Geolocation for c# web app - need country location to set what user can see or not

开发者 https://www.devze.com 2023-04-10 04:51 出处:网络
My scenario is I want to trap the user\'s country when they access the web page.Based on the user\'s country i want to set a cookie that will allow me to do a simple \"if\" statement in the code behin

My scenario is I want to trap the user's country when they access the web page. Based on the user's country i want to set a cookie that will allow me to do a simple "if" statement in the code behind that would let them to see certain information on web pages. I started doing some research and i can use the Google Geocode V3 and get the country from code behind c#. But to get the Google G开发者_JAVA百科eocode to work i need to pass in the longitude and latitude. And the only way i've seen this is from Javascript call like:

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    });
}

So i'm hoping for some advice if I'm doing this right. On the loaded method in my c# code I think I can get the javascript to run by ScriptManager.RegisterStartUpScript method. So my challenge now is getting the latitude and longitude values. Since i'm not going a button event or something, i need to get those values out and then pass them into my c# method that would get the country code using the Google Geocode. Does that sound right? If this is the right path, can someone suggest how i could get those values from the javascript? thanks.


You could make an Ajax call to your server which contains the client's geolocation.

What about using RegionInfo.CurrentRegion instead? That will give you the country according to the browser headers assuming you have globalization culture turned to auto. This is simpler, but will also not be 100% correct all the time since the user can change their culture settings.

You may also want to look into using IP address based geolocation which might be a little more accurate.

It would help to know why you're trying to do this.


Most sites that really need to make sure that content stays within national boundaries work with content that is chargeable - for instance, most music sites, Amazon, and so on.

Their simple, yet effective, technique is to use the country on the user's billing address on their credit card.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号