c# - 如何限制浏览器

标签 c# javascript html asp.net-mvc-3

我在 mvc3 中构建了一个网站,我想在 firefox 上限制我的网站。

我的意思是说,当任何人在 Firefox 上打开我的网站时,它会正确打开,但当任何人在 chrome 或 IE 上打开它时,它会出现自定义错误。我正在使用 c# 和 mvc3

最佳答案

你可以写一个global action filter这将测试 User-Agent HTTP 请求 header :

public class FireFoxOnlyAttribute : ActionFilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var userAgent = filterContext.HttpContext.Request.Headers["User-Agent"];
        if (!IsFirefox(userAgent))
        {
            filterContext.Result = new ViewResult
            {
                ViewName = "~/Views/Shared/Unauthorized.cshtml"
            };
        }
    }

    private bool IsFirefox(string userAgent)
    {
        // up to you to implement this method. You could use
        // regular expressions or simple IndexOf method or whatever you like
        throw new NotImplementedException();
    }
}

然后在 Global.asax 中注册这个过滤器:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new FireFoxOnlyAttribute());
}

关于c# - 如何限制浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8148367/

相关文章:

c# - asp.net mvc 模型与(数据库映射)对象相同吗?

javascript - 如何像facebook wall一样显示输入的url的页面预览?

javascript - 如何在 Javascript 中响应鼠标按钮点击?

javascript - 如何在用户离开(关闭)页面时发送 AJAX 请求?

html - 使用 Flexbox 垂直居中标题元素和内联 div 组

html - Css 无法正确呈现

c# - 属性是图像列表 c#

c# - 如何停止脚本?

c# - 在 Visual Studio 中向项目添加图像

javascript - 在react中使用map函数时如何使用if语句插入索引?