2016-09-04 86 views

回答

1

請記住,谷歌Chrome,IE,Forefox和所有其他瀏覽器也是我們用它來訪問websites.If Windows應用程序要阻止訪問特定的瀏覽器\用戶againt您可以創建一個MVC過濾器,並檢查以下HttpContext屬性:

filterContext.HttpContext.Request.UserAgent; 
//Returns something like - Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 

OR

string browser = filterContext.HttpContext.Request.Browser.Browser; 
//Returns something like - Chrome 

創建以下過濾器,確定從Windows應用程序調用網站時用戶代理的位置,並放入將在用戶遇到特定用戶代理或拋出未授權HTTP狀態代碼時重定向用戶的邏輯:

public class HomeController : Controller 
{ 
    [CallerInterceptorAttribute] 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 
public class CallerInterceptorAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     string userAgent = filterContext.HttpContext.Request.UserAgent; 
     string browser = filterContext.HttpContext.Request.Browser.Browser; 

     if (browser == "Chrome") 
     { 
      filterContext.RequestContext.HttpContext.Response.Redirect("http://www.google.com", true); 
     } 
     base.OnActionExecuting(filterContext); 
    } 
} 

動作過濾器上方可以在控制器級別,動作或全局到整個應用程序被應用

相關問題