mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-08 12:21:07 +00:00
Update projects
This commit is contained in:
parent
44dc36430a
commit
93301110b7
101 changed files with 23635 additions and 16252 deletions
33
src/Tribufu.AspNetCore/Extensions/HttpContextExtensions.cs
Normal file
33
src/Tribufu.AspNetCore/Extensions/HttpContextExtensions.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Tribufu.AspNetCore.Extensions
|
||||
{
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
public static string GetIpAddress(this HttpContext context)
|
||||
{
|
||||
var headers = context.Request.Headers;
|
||||
if (headers.TryGetValue("CF-Connecting-IP", out var cfConnectingIp))
|
||||
{
|
||||
return cfConnectingIp.FirstOrDefault() ?? "127.0.0.1";
|
||||
}
|
||||
|
||||
if (headers.TryGetValue("X-Forwarded-For", out var xForwardedFor))
|
||||
{
|
||||
var forwardedIps = xForwardedFor.ToString().Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (forwardedIps.Length > 0)
|
||||
{
|
||||
return forwardedIps[0].Trim();
|
||||
}
|
||||
}
|
||||
|
||||
return context.Connection.RemoteIpAddress?.ToString() ?? "127.0.0.1";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue