.NET/C#实现识别用户访问设备的方法
程序员文章站
2022-06-07 10:01:50
本文实例讲述了.net/c#实现识别用户访问设备的方法。分享给大家供大家参考,具体如下:
一、需求
需要获取到用户访问网站时使用的设备,根据不同设备返回不同类型的渲染页...
本文实例讲述了.net/c#实现识别用户访问设备的方法。分享给大家供大家参考,具体如下:
一、需求
需要获取到用户访问网站时使用的设备,根据不同设备返回不同类型的渲染页面。
二、实现前准备
通过nuget把uaparser程序包添加到项目中
三、实现
新建uaparseuseragent类文件,在这个文件中进行实现。
实现代码如下:
public class uaparseruseragent { private readonly static uap.parser s_uap; private static readonly regex s_pdfconverterpattern = new regex(@"wkhtmltopdf", regexoptions.compiled | regexoptions.ignorecase | regexoptions.cultureinvariant); # region mobile uas, os & devices private static readonly hashset<string> s_mobileos = new hashset<string> { "android", "ios", "windows mobile", "windows phone", "windows ce", "symbian os", "blackberry os", "blackberry tablet os", "firefox os", "brew mp", "webos", "bada", "kindle", "maemo" }; private static readonly hashset<string> s_mobilebrowsers = new hashset<string> { "android", "firefox mobile", "opera mobile", "opera mini", "mobile safari", "amazon silk", "webos browser", "microb", "ovi browser", "netfront", "netfront nx", "chrome mobile", "chrome mobile ios", "uc browser", "tizen browser", "baidu explorer", "qq browser mini", "qq browser mobile", "ie mobile", "polaris", "one browser", "ibrowser mini", "nokia services (wap) browser", "nokia browser", "nokia oss browser", "blackberry webkit", "blackberry", "palm", "palm blazer", "palm pre", "teleca browser", "semc-browser", "playstation portable", "nokia", "maemo browser", "obigo", "bolt", "iris", "up.browser", "minimo", "bunjaloo", "jasmine", "dolfin", "polaris", "skyfire" }; private static readonly hashset<string> s_mobiledevices = new hashset<string> { "blackberry", "mi pad", "iphone", "ipad", "ipod", "kindle", "kindle fire", "nokia", "lumia", "palm", "docomo", "hp touchpad", "xoom", "motorola", "generic feature phone", "generic smartphone" }; #endregion private readonly httpcontextbase _httpcontext; private string _rawvalue; private useragentinfo _useragent; private deviceinfo _device; private osinfo _os; private bool? _isbot; private bool? _ismobiledevice; private bool? _istablet; private bool? _ispdfconverter; static uaparseruseragent() { s_uap = uap.parser.getdefault(); } public uaparseruseragent(httpcontextbase httpcontext) { this._httpcontext = httpcontext; } public string rawvalue { get { if (_rawvalue == null) { if (_httpcontext.request != null) { _rawvalue = _httpcontext.request.useragent.tostring(); } else { _rawvalue = ""; } } return _rawvalue; } // for (unit) test purpose set { _rawvalue = value; _useragent = null; _device = null; _os = null; _isbot = null; _ismobiledevice = null; _istablet = null; _ispdfconverter = null; } } public virtual useragentinfo useragent { get { if (_useragent == null) { var tmp = s_uap.parseuseragent(this.rawvalue); _useragent = new useragentinfo(tmp.family, tmp.major, tmp.minor, tmp.patch); } return _useragent; } } public virtual deviceinfo device { get { if (_device == null) { var tmp = s_uap.parsedevice(this.rawvalue); _device = new deviceinfo(tmp.family, tmp.isspider); } return _device; } } public virtual osinfo os { get { if (_os == null) { var tmp = s_uap.parseos(this.rawvalue); _os = new osinfo(tmp.family, tmp.major, tmp.minor, tmp.patch, tmp.patchminor); } return _os; } } public virtual bool isbot { get { if (!_isbot.hasvalue) { _isbot = _httpcontext.request.browser.crawler || this.device.isbot; } return _isbot.value; } } public virtual bool ismobiledevice { get { if (!_ismobiledevice.hasvalue) { _ismobiledevice = s_mobileos.contains(this.os.family) || s_mobilebrowsers.contains(this.useragent.family) || s_mobiledevices.contains(this.device.family); } return _ismobiledevice.value; } } public virtual bool istablet { get { if (!_istablet.hasvalue) { _istablet = regex.ismatch(this.device.family, "ipad|kindle fire|nexus 10|xoom|transformer|mi pad|ideatab", regexoptions.cultureinvariant) || this.os.family == "blackberry tablet os"; } return _istablet.value; } } public virtual bool ispdfconverter { get { if (!_ispdfconverter.hasvalue) { _ispdfconverter = s_pdfconverterpattern.ismatch(this.rawvalue); } return _ispdfconverter.value; } } } public sealed class deviceinfo { public deviceinfo(string family, bool isbot) { this.family = family; this.isbot = isbot; } public override string tostring() { return this.family; } public string family { get; private set; } public bool isbot { get; private set; } } public sealed class osinfo { public osinfo(string family, string major, string minor, string patch, string patchminor) { this.family = family; this.major = major; this.minor = minor; this.patch = patch; this.patchminor = patchminor; } public override string tostring() { var str = versionstring.format(major, minor, patch, patchminor); return (this.family + (!string.isnullorempty(str) ? (" " + str) : null)); } public string family { get; private set; } public string major { get; private set; } public string minor { get; private set; } public string patch { get; private set; } public string patchminor { get; private set; } private static string formatversionstring(params string[] parts) { return string.join(".", (from v in parts where !string.isnullorempty(v) select v).toarray<string>()); } } public sealed class useragentinfo { public useragentinfo(string family, string major, string minor, string patch) { this.family = family; this.major = major; this.minor = minor; this.patch = patch; } public override string tostring() { var str = versionstring.format(major, minor, patch); return (this.family + (!string.isnullorempty(str) ? (" " + str) : null)); } public string family { get; private set; } public string major { get; private set; } public string minor { get; private set; } public string patch { get; private set; } } internal static class versionstring { public static string format(params string[] parts) { return string.join(".", (from v in parts where !string.isnullorempty(v) select v).toarray<string>()); } }
控制器中代码:
uaparseruseragent useragent = new uaparseruseragent(this.httpcontext); dto.osinfo = useragent.os.tostring(); dto.device = useragent.device.tostring() != "other" ? useragent.device.tostring() : "电脑"; dto.agent = useragent.useragent.tostring(); dto.rawvalue = useragent.rawvalue.tostring(); //if (useragent.ismobiledevice) //{ // debug.writeline("这是一个手机"); // viewbag.mobilepc = "手机"; //} //else if (useragent.istablet) //{ // viewbag.mobilepc = "平板"; // debug.writeline("这是一个平板"); //} //else //{ // viewbag.mobilepc = "普通电脑"; // debug.writeline("这是一个普通电脑"); //}
更多关于c#相关内容感兴趣的读者可查看本站专题:《c#程序设计之线程使用技巧总结》、《winform控件用法总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。