Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

detect location from ip address .net core

// Install-Package MaxMind.GeoIP2

// controller
public class HomeController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public IActionResult Index()
    {
        using (var reader = new DatabaseReader(_hostingEnvironment.ContentRootPath + "GeoLite2-City.mmdb"))
        {
            // Determine the IP Address of the request
            var ipAddress = HttpContext.Connection.RemoteIpAddress;

            // Get the city from the IP Address
            var city = reader.City(ipAddress);

            return View(city);
        }
    }
}
// razor
@model MaxMind.GeoIP2.Responses.CityResponse
@{
    ViewData["Title"] = "Home Page";
}

<div class="row" style="margin-top: 50px;">
    <div class="col-md-12">
        <p>Hey there, welcome visitor from <strong>@Model.City, @Model.Country!</strong></p>
        <p>Your time zone is <strong>@Model.Location.TimeZone</strong></p>
    </div>
</div>

// startup or now program file
public class Startup
{
    // Some code omitted from this code sample for brevity...

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor,

                // IIS is also tagging a X-Forwarded-For header on, so we need to increase this limit, 
                // otherwise the X-Forwarded-For we are passing along from the browser will be ignored
                ForwardLimit = 2 
            });

            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: asp.net core get current culture in controller 
Csharp :: checkbox on change c# xamarin forms 
Csharp :: C# Relational Operators 
Csharp :: Return out of a Ienumerator/Courotine in C# 
Csharp :: best unity regex for email validation in c# 
Csharp :: c# if combobox selected index 
Csharp :: player movement unity 3d script 
Csharp :: paging thru result from mongodb in C# 
Csharp :: c# ile ürün çekme - htmlagilitypack 
Csharp :: open full screen wpf 
Csharp :: c# regex double of some letters only 
Csharp :: open html file in browser using c++ 
Csharp :: create blazor web assembly 
Csharp :: C# data base sql 
Csharp :: Xamarin Forms iOS Picker done 
Csharp :: function documentation c# exception 
Csharp :: c# convert timestamp to datetime 
Csharp :: MVC Razor check for postback 
Csharp :: block wapalyzer from detecting codeigniter 
Csharp :: SonarQube UnitTests 
Csharp :: C# walk down a tree and back 
Csharp :: NetConnectionDispatch 
Csharp :: 403 forbidden error using Windows Forms 
Csharp :: How to enumerate an enum 
Csharp :: c# capitalize first letter of each word 
Csharp :: true false when key pressed in c sharp unity 
Csharp :: c# ipaddress to integer 
Csharp :: my custom file watcher 
Csharp :: enable asnotracking in asp.net core at global level 
Csharp :: C# console out restore 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =