我正在使用动作过滤器在我的项目中完成一项工作。我想这样做,如果用户的ip等于我的ip,它将转到索引而不会看到登录页面。如果它的IP不同,我想将他重定向到登录页面。在登录页面中,我要求输入密码和ID。我有重定向到登录页面的问题。这是我的代码,我该如何修复这个循环?
public class IntranetAction : ActionFilterAttribute
{
private const string LOCALIP = "192.168";
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)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Login", //TODO - Edit as per you controller and action
action = "User"
}));
}
else
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Home", //TODO - Edit as per you controller and action
action = "Index"
}));
}
base.OnActionExecuting(filterContext);
}
}
public class LoginController : Controller
{
// GET: Login
[IntranetAction]
public ActionResult User()
{
return View();
}
public void checkAuthentication(UserLoginInfo loginInfo)
{
bool isAuthenticated = new LdapServiceManager().isAuthenticated(loginInfo);
if (isAuthenticated)
{
//HttpContext.Response.Redirect("/Home/Index");
Response.Redirect("/Home/Index");
Response.End();
}
else
{
Response.Redirect("/", false);
}
}
}
我的过滤器类中的这个循环。 shortLocalIP不等于我的LOCALIP,它会进入我的登录页面,但它会进入inf循环
答案 0 :(得分:1)
我认为您需要在登录控制器中另一个视图Index
。
如果user ip
和your ip
相等,请转到Home/Index
,否则请转到Login/Index
。
您的startup
视图将Login/User
放置filter
。
public class IntranetAction : ActionFilterAttribute
{
private const string LOCALIP = "192.168";
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)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Login", //TODO - Edit as per you controller and action
action = "Index"
}));
}
else
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Home", //TODO - Edit as per you controller and action
action = "Index"
}));
}
base.OnActionExecuting(filterContext);
}
}