c# , .net 后端判断是否是移动端,是否是安卓设备,是否是微信,是否是支付宝,是否是QQ,是否是安卓系统,是否是苹果系统等

别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述c# , .net 后端判断是否是移动端,是否是安卓设备,是否是微信,是否是支付宝,是否是QQ,是否是安卓系统,是否是苹果系统等相关的知识,希望能为你提供帮助。


【c# , .net 后端判断是否是移动端,是否是安卓设备,是否是微信,是否是支付宝,是否是QQ,是否是安卓系统,是否是苹果系统等】?仅支持web项目


第一步:拿到请求上下文方法一:
var UserAgentText= Request.UserAgent;

方法二:
var request = Request.Headers;
string UserAgentText= request["User-Agent"];

第二步:获取设备信息
UserAgentDeviceInfoModel userAgentDeviceInfoModel = UserAgentHelp.GetDeviceInfo(UserAgentText);

示例:
public class UserAgentExecute

public static void Execute()

List< string> listStr = new List< string> ();

/【电脑-谷歌浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/88.0.4324.104 Safari/537.36");

/【电脑-360浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");

/【手机微信-浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (Linux; android 10; M2002J9E Build/QKQ1.191222.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/3209 MMWEBSDK/20220204 Mobile Safari/537.36 MMWEBID/3181 MicroMessenger/8.0.20.2100(0x2800149A) Process/toolsmp WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64");

/【苹果手机微信-浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x1800123f) NetType/WIFI Language/zh_CN");

/【手机QQ-浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (Linux; Android 10; M2002J9E Build/QKQ1.191222.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.72 MQQBrowser/6.2 TBS/045913 Mobile Safari/537.36 V1_AND_SQ_8.8.83_2654_YYB_D A_8088300 PA QQ/8.8.83.7540 NetType/WIFI WebP/0.3.0 Pixel/1080 StatusBarHeight/70 SimpleUISwitch/0 QQTheme/1000 InMagicWin/0 StudyMode/0 CurrentMode/0 CurrentFontScale/1.0 GlobalDensityScale/0.9818182 AppId/537114460");

/【苹果手机-QQ-浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19E241 QQ/8.8.85.636 V1_IPH_SQ_8.8.85_1_APP_A Pixel/1170 MiniAppEnable SimpleUISwitch/1 StudyMode/0 CurrentMode/1 CurrentFontScale/1.000000 QQTheme/2921 AppId/537115277 Core/WKWebView Device/Apple(iPhone 13) NetType/WIFI QBWebViewType/1 WKType/1");

/【手机支付宝-浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (Linux; U; Android 10; zh-CN; M2002J9E Build/QKQ1.191222.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 UWS/3.22.2.43 Mobile Safari/537.36 UCBS/3.22.2.43_220223200704 NebulaSDK/1.8.100112 Nebula AlipayDefined(nt:WIFI,ws:393|0|2.75) AliApp(AP/10.2.58.8000) AlipayClient/10.2.58.8000 Language/zh-Hans useStatusBar/true isConcaveScreen/true Region/CNAriver/1.0.0");

/【苹果手机-支付宝-浏览器信息】User-Agent/
listStr.Add("Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19E241 Ariver/1.1.0 AliApp(AP/10.2.59.2500) Nebula WK RVKType(1) AlipayDefined(nt:WIFI,ws:390|780|3.0) AlipayClient/10.2.59.2500 Alipay Language/zh-Hans Region/CN NebulaX/1.0.0");


UserAgentDeviceInfoModel userAgentDeviceInfoModel = null;
foreach (string UserAgentText in listStr)

userAgentDeviceInfoModel = UserAgentHelp.GetDeviceInfo(UserAgentText);
Console.WriteLine(userAgentDeviceInfoModel.Remark);





结果:
系统:Windows;应用名称:unknown;是否移动端:False;
系统:Windows;应用名称:unknown;是否移动端:False;
系统:Android;应用名称:WeChat;是否移动端:True;
系统:iPhone;应用名称:WeChat;是否移动端:True;
系统:Android;应用名称:QQ;是否移动端:True;
系统:iPhone;应用名称:QQ;是否移动端:True;
系统:Android;应用名称:Alipay;是否移动端:True;
系统:iPhone;应用名称:Alipay;是否移动端:True;



核心帮助类UserAgentHelp:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test1.Request.UserAgent

public class UserAgentHelp

/// < summary>
/// 获取设备信息
/// < /summary>
/// < param name="UserAgentText"> < /param>
/// < returns> < /returns>
public static UserAgentDeviceInfoModel GetDeviceInfo(string UserAgentText)

UserAgentDeviceInfoModel userAgentDeviceInfoModel = new UserAgentDeviceInfoModel()

System = ChackSystemInfo(UserAgentText),
App = ChackAppInfo(UserAgentText),
IsMobile = UserAgentText.Contains("Mobile")
;
userAgentDeviceInfoModel.Remark = $"系统:userAgentDeviceInfoModel.System;应用名称:userAgentDeviceInfoModel.App;是否移动端:userAgentDeviceInfoModel.IsMobile;";

return userAgentDeviceInfoModel;

/// < summary>
/// 检查系统信息
/// < /summary>
/// < param name="str"> < /param>
/// < returns> < /returns>
private static DeviceSystemEnum ChackSystemInfo(string str)

if (str.Contains("Android"))

return DeviceSystemEnum.Android;

else if (str.Contains("iPhone"))

return DeviceSystemEnum.iPhone;

else if (str.Contains("Windows"))

return DeviceSystemEnum.Windows;

else

return DeviceSystemEnum.unknown; //未知


/// < summary>
/// 检查应用信息
/// < /summary>
/// < param name="str"> < /param>
/// < returns> < /returns>
private static DeviceAppEnum ChackAppInfo(string str)

if (str.Contains("MicroMessenger"))

return DeviceAppEnum.WeChat;

else if (str.Contains("AlipayClient"))

return DeviceAppEnum.Alipay;

else if (str.Contains("QQ"))

return DeviceAppEnum.QQ;

else

return DeviceAppEnum.unknown; //未知



/// < summary>
/// 用户请求的设备信息
/// < /summary>
public class UserAgentDeviceInfoModel

/// < summary>
/// 系统信息
/// < /summary>
public DeviceSystemEnum Systemset; get;
/// < summary>
/// 应用信息
/// < /summary>
public DeviceAppEnum Appset; get;
/// < summary>
/// 是否移动端
/// < /summary>
public bool IsMobilese

    推荐阅读