Asp.net 使用Global.asax设置/检查会话变量并重定向(用于用户测试)
我想添加非常简单,临时安全到我的网站 我在Home/Understruction中创建了一个页面,测试站点的人可以输入硬编码密码,然后将“Understruction”会话变量设置为“false” 到目前为止,我已经做到了这一点,但这会导致太多重定向:Asp.net 使用Global.asax设置/检查会话变量并重定向(用于用户测试),asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我想添加非常简单,临时安全到我的网站 我在Home/Understruction中创建了一个页面,测试站点的人可以输入硬编码密码,然后将“Understruction”会话变量设置为“false” 到目前为止,我已经做到了这一点,但这会导致太多重定向: protected void Session_Start(Object sender, EventArgs e) { HttpContext.Current.Session["underconstruction"]
protected void Session_Start(Object sender, EventArgs e)
{
HttpContext.Current.Session["underconstruction"] = "true";
}
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
var underconstruction = HttpContext.Current.Session["underconstruction"];
if (underconstruction != null)
{
string oc = underconstruction.ToString();
if (oc != "false") Response.Redirect("~/Home/UnderConstruction");
}
}
}
这接近我需要做的吗
下面是我们需要处理的代码:
欠施工视图的控制器代码
public ViewResult UnderConstruction()
{
return View();
}
[HttpPost]
public ActionResult UnderConstruction(string ocp)
{
if (ocp == "mypassword")
{
Session["underconstruction"] = "false";
return RedirectToAction("Index", "Home");
}
else
{
Session["beingredirected"] = "false";
return View();
}
}
Global.Asax
protected void Session_Start(Object sender, EventArgs e)
{
HttpContext.Current.Session["underconstruction"] = "true";
HttpContext.Current.Session["beingredirected"] = "false";
}
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
bool uc = false;
var underconstruction = HttpContext.Current.Session["underconstruction"];
if (underconstruction != null)
{
uc = Boolean.Parse(underconstruction.ToString());
}
bool redirected = false;
var beingredirected = HttpContext.Current.Session["beingredirected"];
if (beingredirected != null)
{
redirected = Boolean.Parse(beingredirected.ToString());
}
if (uc && !redirected)
{
if (Request.HttpMethod == "GET")
{
HttpContext.Current.Session["beingredirected"] = "true";
Response.Redirect("~/Home/UnderConstruction");
}
else if (Request.HttpMethod == "POST")
{
}
}
HttpContext.Current.Session["beingredirected"] = "false";
}
}
~/Home/understruction
是否在其他网站中?如果不是,它是否总是重定向,因为oc
将始终为真?ie-您是否还需要为正在请求的页面添加检查,以便在已经转到欠构建
页面时可以绕过重定向
更新
不确定检查页面名称是否是一个好主意,但类似的方法可能会奏效:
protected void Session_Start(Object sender, EventArgs e)
{
HttpContext.Current.Session["underconstruction"] = "true";
HttpContext.Current.Session["beingredirected"] = "false";
}
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
bool uc = false;
var underconstruction = HttpContext.Current.Session["underconstruction"];
if (underconstruction != null)
{
uc = Boolean.Parse(underconstruction);
}
bool redirected = false;
var beingredirected = HttpContext.Current.Session["beingredirected"];
if (beingredirected != null)
{
redirected = Boolean.Parse(beingredirected);
}
if (uc && !redirected)
{
HttpContext.Current.Session["beingredirected"] = "true";
Response.Redirect("~/Home/UnderConstruction");
}
HttpContext.Current.Session["beingredirected"] = "false";
}
}
请注意,我会整理一下,这个例子只是给出了一般的想法
更新
如果您想使用评论中提到的角色,则可能会有所帮助。这有点复杂,但是没有像上面的解决方案那样引入临时代码有额外的好处吗您试过了吗?这就是我要开始的地方,但正如我所说,这会导致一个无止境的循环。我不知道如何修复它。只是想一想,但是你能把你的if语句改成
if(oc==“true”)
吗,因为你没有为了比较而修剪或降低它的大小写?我仍然在Chrome中收到消息“这个网页有一个重定向循环”。我认为在其他浏览器中也会出现同样的问题。新的答案似乎有帮助,但当我第二次进入页面时,没有输入密码,它会让我进入。快到了。我正在研究它,很快就会发回。@MightMuke,如果我理解正确,那么你在这里隐藏了最初的错误。“重定向太多”的原始错误是因为在计算了几个条件并设置了会话后,您正在global.asax上导航到Understruction页面。在下一轮综述中,global asax将再次经历相同的周期。没有出口。Session_Start事件不用于创建会话,它包含会话启动时发生的逻辑。@CodeSpread是的,很可能是。我只是简单地指出,除非添加一个检查以允许导航到正在构建的页面,否则您将进入一个重定向循环。我的代码当然不好看。听上去,我怀疑您有一个更好的解决方案:)@Mightymuke,这里的临时安全问题可以通过使用web.config“”节来实现。Mightymuke,我做了一些轻微的调整,因为.asax代码阻止了post的发生(需要设置Understruction会话变量)。还必须重置视图中的其他会话变量。我把我的变化贴在上面。没有你的帮助,我不可能做这件事。谢谢