public enum OSTypeEnum
{
Windows,
Linux,
MacOS,
Other
}
#region 浏览器操作系统与网站部署所在操作系统不一致 不让登录
string userAgent = HttpContext.Current.Request.UserAgent;
OSTypeEnum browserOSTypeEnum = new OSTypeEnum();//当前访问浏览器所在操作系统
// 推断操作系统
if (userAgent.Contains("Windows NT"))
{
browserOSTypeEnum = OSTypeEnum.Windows;
}
else if (userAgent.Contains("Mac OS X"))
{
browserOSTypeEnum = OSTypeEnum.MacOS;
}
else if (userAgent.Contains("Linux"))
{
browserOSTypeEnum = OSTypeEnum.Linux;
}
else
{
browserOSTypeEnum = OSTypeEnum.Other;
}
OSTypeEnum ServerTypeEnum = new OSTypeEnum();//网站部署所在操作系统
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ServerTypeEnum = OSTypeEnum.Windows;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
ServerTypeEnum = OSTypeEnum.Linux;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
ServerTypeEnum = OSTypeEnum.MacOS;
}
else
{
ServerTypeEnum = OSTypeEnum.Other;
}
if (browserOSTypeEnum != ServerTypeEnum)//浏览器操作系统与网站部署所在操作系统不一致 不让登录
{
context.Response.Write($@"当前浏览器操作系统{browserOSTypeEnum}与网站部署所在操作系统{ServerTypeEnum}不一致,禁止登录!(请使用{ServerTypeEnum}操作系统登录)");
return;
}
#endregion