![](/img/trans.png)
[英]How to restrict user in opening multiple Tabs in a browser while performing tasks?
[英]How to Restrict Browser
我在mvc3中建立了一個網站,我想在firefox上限制我的網站。
我的意思是說,當有人在firefox上打開我的網站時,它可以正確打開,但是當有人在chrome或IE上打開它時,則會出現定制錯誤。 我在mvc3中使用c#
您可以編寫一個全局操作過濾器 ,該過濾器將測試User-Agent HTTP請求標頭:
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());
}
您正在尋找連接到您網站的用戶的用戶代理,可以通過控制器中的此調用來檢索該用戶代理:
Request.UserAgent
不過,我並不同意這種模式。
這是一個簡單的javascript函數,您可以將其添加到代碼中並對其執行操作。
function detect_browser() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("firefox") != -1) return true;
else{
window.location="";//Here within quotes write the location of your error page.
}
}
在主頁上,您可以調用頁面加載事件函數。 雖然不建議這種做法。
您可以將Request.UserAgent測試為路由約束的一部分。
例如,您可以如下定義路線約束例程:
public class UserAgentConstraint : IRouteConstraint
{
private string requiredUserAgent;
public UserAgentConstraint(string agentParam)
{
requiredUserAgent = agentParam;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.UserAgent != null && httpContext.Request.UserAgent.Contains(requiredUserAgent);
}
}
然后將以下約束添加到路由:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, //Parameter defaults
new { customConstraint = new UserAgentConstraint("Firefox") } //Constraint
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.