我正在使用动作过滤器在我的项目中做一项工作。我想这样做,如果用户的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);
}
}
}这是我的filter类中的循环。shortLocalIP不等于我的LOCALIP,它转到我的登录页面,但它转到inf循环
发布于 2016-08-29 15:04:24
我认为你需要在登录控制器中添加另一个视图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);
}
}https://stackoverflow.com/questions/39200176
复制相似问题