欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET判断是否为手机登录

程序员文章站 2022-06-24 12:37:19
页面加载时候判断是否为手机登录 ......
        protected void Page_Load(object sender, EventArgs e)
        {
            MobileHandle();
           }

页面加载时候判断是否为手机登录

        protected void MobileHandle()
        {
            string mobilePath = PublicFunction.GetConfigValue("MobilePath");//手机页面的路径
            if (!string.IsNullOrEmpty(mobilePath))
            {
                bool isMobile = PublicFunction.IsMobile(Request.UserAgent);
                if (isMobile)
                {
                    Response.Redirect(mobilePath);//为手机登录的话跳转手机页面
                }
            }
        }
  public class PublicFunction
    {
        static string[] mobileTag = { "iphone", "ios", "ipad", "android", "mobile" };

        /// <summary>
        /// 判断是否是手机打开
        /// </summary>
        /// <param name="userAgent">用户浏览器代理信息</param>
        /// <returns></returns>
        public static bool IsMobile(string userAgent)
        {
            bool result = false;
            userAgent = userAgent.ToLower();
            foreach (string sTmp in mobileTag)
            {
                if (userAgent.Contains(sTmp))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
}