30 lines
877 B
C#
30 lines
877 B
C#
namespace Apimanager_backend.Tools
|
||
{
|
||
public static class UserInfoHelper
|
||
{
|
||
public static string GetClientIp(HttpContext context)
|
||
{
|
||
var ip = context.Connection.RemoteIpAddress?.ToString();
|
||
|
||
// 支持代理(如 Nginx)时使用 X-Forwarded-For
|
||
if (context.Request.Headers.ContainsKey("X-Forwarded-For"))
|
||
{
|
||
ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||
}
|
||
|
||
return ip ?? "unknown";
|
||
}
|
||
public static string GetDeviceType(HttpContext context)
|
||
{
|
||
var userAgent = context.Request.Headers["User-Agent"].ToString().ToLower();
|
||
|
||
if (userAgent.Contains("mobile") || userAgent.Contains("android") || userAgent.Contains("iphone"))
|
||
return "mobile";
|
||
|
||
return "pc";
|
||
}
|
||
|
||
|
||
}
|
||
}
|