2016-08-26 17 views
2

我們正在嘗試ASP.NET MVC項目中的一些登錄操作。我們的目標是:「如果用戶的IP是不是從我們的內部網,他/她重定向到登錄頁面。否則,只要到我們的索引頁面,我們寫了一些代碼,但我們是在一個循環中Application_BeginRequest用法

GLOBAL。。 ASAX

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     var request = ((System.Web.HttpApplication) sender).Request; 

     string ip1 = request.UserHostAddress; 
     string shortLocalIP; 
     if (ip1 != null && ip1.Contains(".")) 
     { 
      string[] ipValues = ip1.Split('.'); 
      shortLocalIP = ipValues[0] +"."+ipValues[1]; 
     } 
     else 
     { 
      shortLocalIP = "192.168"; 
     } 

     //var ip2 = request.ServerVariables["LOCAL_ADDR"]; 
     //var ip3 = request.ServerVariables["SERVER_ADDR"]; 

     if (shortLocalIP != LOCALIP) 
     { 
      if ("/Login/User".Equals(request.RawUrl, StringComparison.InvariantCultureIgnoreCase)) 
      {     
        return;         
      } 

      Response.Clear();    
      Response.Redirect("/Login/User");    
      Response.End();  
     } 
    } 

登錄控制器

public class LoginController : Controller 
{ 
    // GET: Login 
    public ActionResult User() 
    {   
     return View(); 
    } 

    public ActionResult checkAuthentication(FormCollection collection) 
    { 
     bool isAuthenticated = new LdapServiceManager().isAuthenticated(collection); 
     if (isAuthenticated) 
     { 
      Response.Redirect("Home/Index"); 
     } 
     else 
     { 
      Response.Redirect("/Login/User"); 
     } 
     return null; 
    } 
} 

LOGIN CSHTML

<form class="login-form" action="/Login/checkAuthentication" method="post" novalidate="novalidate"> 

當我們按下某個按鈕或其他東西時,Application_BeginRequest會每次觸發。但是我們只是在開始時才需要這些操作。謝謝...

我們應該在GLOBAL.ASAX中使用SESSION START嗎?

+0

你是什麼意思「在開始」?這個用戶第一次打開你的應用程序?但那麼什麼是「這個用戶」?來自此特定IP的用戶?那麼你需要檢查每個請求的IP ......這正是你在做什麼! – Felix

+0

我的意思是如果你打開我的頁面,我們檢查你的IP。如果您的IP來自我們的網絡,請轉到索引。如果您的IP不是來自我們的網絡,請將頁面指向loginPage。在loginPage中,我們需要來自用戶的輸入。當用戶按登錄按鈕時,由於application_beginRequest – Berkin

回答

2

您可以使用ActionFilter這一點。創建自定義過濾器類,像這樣 -

public class IntranetAction : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     bool isIPAddressValid = false;//TODO: Check the IP validity here 
     if (isIPAddressValid) 
     { 
      filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new 
      { 
       controller = "Account", //TODO - Edit as per you controller 
       action = "Login"  //and Action 
      })); 
     } 

     base.OnActionExecuting(filterContext); 
    } 
} 

,簡單地用它在你的控制器ActionMethod像這樣的例子 -

[IntranetAction] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

Suugest要經過好文章開始使用定製過濾器 - http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

+0

我不能在我的函數頂部寫上IntranetAction – Berkin

+0

@Berkin'IntranetAction'是我們創建的自定義過濾器。確保名稱正確,並且引用了正確的名稱空間。 – Yogi

+0

是的,謝謝,它解決了:) – Berkin

0

Application_BeginRequest將在每次向服務器發出的請求中調用。如果您只想在某些動作上執行某些邏輯,請使用ActionFilters

+0

,它再次重定向loginPage謝謝,我會查找它謝謝 – Berkin

1

您可以使用ActionFilter for MVC。這是操作的示例代碼。

public class IpControlAttribute : ActionFilterAttribute { 
    private const string LOCALIP = ""; 

    public override void OnActionExecuting(ActionExecutingContext filterContext) { 
     var request = filterContext.RequestContext.HttpContext.Request; 

     string ip1 = request.UserHostAddress; 
     string shortLocalIP; 
     if (ip1 != null && ip1.Contains(".")) { 
      string[] ipValues = ip1.Split('.'); 
      shortLocalIP = ipValues[0] + "." + ipValues[1]; 
     } else { 
      shortLocalIP = "192.168"; 
     } 

     //var ip2 = request.ServerVariables["LOCAL_ADDR"]; 
     //var ip3 = request.ServerVariables["SERVER_ADDR"]; 

     if (shortLocalIP != LOCALIP) { 
      if ("/Login/User".Equals(request.RawUrl, StringComparison.InvariantCultureIgnoreCase)) { 
       return; 
      } 

      filterContext.Result = new RedirectResult("/Account/Login"); 
     } 
    } 
} 

然後,您需要將其添加爲全局篩選器在FilterConfig.cs

filters.Add(new IpCheckAttribute()); 
+0

感謝您的幫助,Teşekkürler:)我創建一個名爲FilterConfig.cs到App_Start文件夾的類。但我不明白最後一部分。我應該在哪裏寫filters.add部分?在global.asax - > Application_Start函數中? – Berkin

+0

請記住,將其添加爲全局過濾器將使其執行所有操作。請小心。 – Yogi

+0

沒有在mvc應用程序中有一個名爲** App_Start **的文件夾。在那之下有** FilterConfig **文件。在那裏添加代碼。另一方面,@Yogi提到它會用於每一個動作。如果您想指定特定操作,您需要將其寫入控制器或類似 '[IpCheck] public class HomeController:Controller {}' –

相關問題