【发布时间】:2017-01-02 18:25:30
【问题描述】:
我们正在我们的 ASP.NET MVC 项目中尝试一些登录操作。我们的目标是:“如果用户的 IP 不是来自我们的 Intranet,则将他/她重定向到登录页面。否则,只需转到我们的索引页面。我们编写了一些代码,但我们处于循环中。
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;
}
}
登录 CSHTML
<form class="login-form" action="/Login/checkAuthentication" method="post" novalidate="novalidate">
Application_BeginRequest 每次我们按下某个按钮或其他东西时都会触发。但我们只希望在开始时进行这些操作。谢谢...
我们应该在 GLOBAL.ASAX 中使用 SESSION START 吗?
【问题讨论】:
-
你是什么意思“开始”?该用户第一次打开您的应用程序?但是,什么是“这个用户”?来自这个特定 IP 的用户?那么你需要检查每个请求的 IP ......这正是你正在做的事情!
-
我的意思是,如果您打开我的页面,我们会检查您的 IP。如果您的 ip 来自我们的网络,请转到 index.html。如果您的 ip 不是来自我们的网络,请将页面定向到 loginPage。在 loginPage 中,我们需要来自用户的输入。当用户按下登录按钮时,由于 application_beginRequest 再次重定向 loginPage
标签: c# asp.net asp.net-mvc