Suppose we have a condition that we don’t want to allow the access of our website in certain parts of the world . That we can do here in ASP.Net .
In the process , it retrieves the country based on the browser . It isn’t going to be 100% accurate but probably more like 80-90% .That’s because some people change the browser language instead of their native language and others use a non-ISO standard language. And last, some clients just don’t send language information.
This process will be done in two main phases : 1. Resolve the culture
It resolves the CultureInfo based on the browsers language.
public static CultureInfo Culture()
{
string[] languages = HttpContext.Current.Request.UserLanguages;
if (languages == null || languages.Length == 0)
return null;
try
{
string language = languages[0].ToLowerInvariant().Trim();
return CultureInfo.CreateSpecificCulture(language);
}
catch (ArgumentException)
{
return null;
}
}
2. Resolve the country
This Method uses ResolveCulture() method above to create a RegionInfo object. The RegionInfo contains all the country information needed such as ISO code, EnglishName, NativeName and DisplayName.
public static RegionInfo country()
{
CultureInfo culture = Culture();
if (culture != null)
return new RegionInfo(culture.LCID);
return null;
}
At Last Suppose we don’t want to give access of the website to people of “US”.
Then In page Load :
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = country().ToString();
if (Label1.Text == "US")
{
Response.Redirect("PageYouWantToRedirect.aspx");
}
}