当前位置: 首页 >>服务端开发 >>Asp.Net >>C#判断用户是否是在使用手机访问

C#判断用户是否是在使用手机访问

时间:2021/2/23 9:07:00 【未知】

在做开发的过程中,很多时候我们都要判断用户的来源,比如手机端或电脑端,再根据来源的结果来编写相应的功能。下面即为判断手机端与电脑端的源码。…

    在做开发的过程中,很多时候我们都要判断用户的来源,比如手机端或电脑端,再根据来源的结果来编写相应的功能。下面即为判断手机端与电脑端的源码:

1.根据操作系统的属性来判断是否用手机访问

public static bool HdhCmsIsMobileAccess()

{

    bool HdhCmsFlag = false;

    string HdhCmsClient = HttpContext.Current.Request.UserAgent;

    string[] keywords = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };

    //首选判断是否为 Windows 桌面系统及IE浏览器的判断,不是WINDOWS是进入苹果系统的判断

    if (!HdhCmsClient.Contains("Windows NT") || (HdhCmsClient.Contains("Windows NT") && HdhCmsClient.Contains("compatible; MSIE 9.0;")))

    {

        //如果不是WINDOWS操作系统,则进入是否苹果操作系统的判断,如果不是则进入到下一步

        if (!HdhCmsClient.Contains("Windows NT") && !HdhCmsClient.Contains("Macintosh"))

        {

            foreach (string HdhCmsValue in keywords)

            {

                if (HdhCmsClient.Contains(HdhCmsValue))

                {

                    HdhCmsFlag = true;

                    break;

                }

            }

        }

    }

    return HdhCmsFlag;

}

2.根据所使用的手机及系统来判断是否为手机访问:

public bool HdhCmsIsMoblie()

        {

            string HdhCmsClient = (Request.UserAgent + "").ToLower().Trim();

            if (HdhCmsClient == "" ||

                HdhCmsClient.IndexOf("mobile") != -1 ||

                HdhCmsClient.IndexOf("mobi") != -1 ||

                HdhCmsClient.IndexOf("nokia") != -1 ||

                HdhCmsClient.IndexOf("samsung") != -1 ||

                HdhCmsClient.IndexOf("sonyericsson") != -1 ||

                HdhCmsClient.IndexOf("mot") != -1 ||

                HdhCmsClient.IndexOf("blackberry") != -1 ||

                HdhCmsClient.IndexOf("lg") != -1 ||

                HdhCmsClient.IndexOf("htc") != -1 ||

                HdhCmsClient.IndexOf("j2me") != -1 ||

                HdhCmsClient.IndexOf("ucweb") != -1 ||

                HdhCmsClient.IndexOf("opera mini") != -1 ||

                HdhCmsClient.IndexOf("mobi") != -1 ||

                HdhCmsClient.IndexOf("android") != -1 ||

                HdhCmsClient.IndexOf("iphone") != -1) 

            {

                //终端可能是手机

                return true;

            }

            return false;

        }

3.根据设备信息来判断是否为手机访问

转自:https://www.cnblogs.com/ghw0501/p/7094845.html

/// <summary>

/// 判断是否移动设备。

/// </summary>

/// <returns></returns>

public static bool IsMobileDevice()

{

    string[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "googlebot-mobile" };

    bool isMoblie = false;

    string userAgent = HttpContext.Current.Request.UserAgent.ToString().ToLower();

    //排除 Windows 桌面系统或苹果桌面系统 

    if (!string.IsNullOrEmpty(userAgent) && !userAgent.Contains("macintosh") && (!userAgent.Contains("windows nt") || (userAgent.Contains("windows nt") && userAgent.Contains("compatible; msie 9.0;"))))

    {

        for (int i = 0; i < mobileAgents.Length; i++)

        {

            if (userAgent.ToLower().IndexOf(mobileAgents[i]) >= 0)

            {

                isMoblie = true;

                break;

            }

        }

    }

    return isMoblie;

}


相关文章

CopyRight:2007-2018 编程语言吧 备案ICP:湘ICP备09009000号-15 http://www.yuyanba.com

分享按钮