Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 应用程序\u开始请求使用_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 应用程序\u开始请求使用

C# 应用程序\u开始请求使用,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我们正在ASP.NET MVC项目中尝试一些登录操作。我们的目标是:“如果用户的IP不是来自我们的内部网,请将他/她重定向到登录页面。否则,只需转到我们的索引页面”。我们编写了一些代码,但我们处于循环中 GLOBAL.ASAX 登录控制器 登录CSHTML 应用程序_BeginRequest每次按下某个按钮或其他东西时都会触发。但我们只希望在开始时执行这些操作。谢谢 我们应该在全局中使用SESSION START吗?尽快???对服务器发出的每个请求都会调用应用程序BeginRequest。如

我们正在ASP.NET MVC项目中尝试一些登录操作。我们的目标是:“如果用户的IP不是来自我们的内部网,请将他/她重定向到登录页面。否则,只需转到我们的索引页面”。我们编写了一些代码,但我们处于循环中

GLOBAL.ASAX 登录控制器 登录CSHTML

应用程序_BeginRequest每次按下某个按钮或其他东西时都会触发。但我们只希望在开始时执行这些操作。谢谢


我们应该在全局中使用SESSION START吗?尽快???

对服务器发出的每个请求都会调用应用程序BeginRequest。如果您只想在某些操作上执行某些逻辑,请使用您可以对MVC使用ActionFilter。下面是该操作的示例代码

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());

您可以为此使用
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);
    }
}
只需在控制器上使用它即可
操作方法
,例如-

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

Suugest阅读一篇好文章,开始使用自定义过滤器-

你说的“开始”是什么意思?这个用户第一次打开你的应用程序时?那么“这个用户”是什么"? 来自此特定IP的用户?然后你需要检查每个请求的IP。。。这正是你正在做的!我的意思是如果你打开我的网页,我们会检查你的IP。如果您的ip来自我们的网络,请转到索引。如果您的ip不是来自我们的网络,请将页面指向loginPage。在登录页面中,我们需要用户的输入。当用户按login按钮时,它会再次重定向loginPage,因为应用程序_BeginRequest感谢您的帮助,Teşekkürler:)我创建了一个名为FilterConfig.cs的类,并将其放入App_Start文件夹。但我不明白最后一部分。我应该在哪里编写过滤器。添加部件?在global.asax->Application\u Start function?中,请记住,将其添加为全局筛选器将使其对所有操作执行。注意。否在mvc应用程序中有一个名为App_Start的文件夹。下面是FilterConfig文件。在那里添加代码。另一方面@Yogi提到它将用于每个动作。如果您想指定特定操作,您需要将其写入控制器或类似于
[IpCheck]公共类HomeController:Controller{}
的操作,回答有点晚!我不能在我的笔记本上写内传function@Berkin
IntranetAction
是我们创建的自定义筛选器。确保名称正确,并且引用的名称空间正确。
filters.Add(new IpCheckAttribute());
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);
    }
}
    [IntranetAction]
    public ActionResult Index()
    {
        return View();
    }