gpt4 book ai didi

asp.net - 使用 Global.asax 设置/检查 session 变量和重定向(用于用户测试)

转载 作者:行者123 更新时间:2023-12-04 11:27:12 25 4
gpt4 key购买 nike

我想为我的网站添加非常简单的临时安全性。

我在 Home/UnderConstruction 上做了一个页面,测试站点的人可以输入一个硬编码的密码,然后将“underconstruction” session 变量设置为“false”。

这是我到目前为止所拥有的,但它导致太多重定向:

    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");
}
}

}

这接近我需要做的吗?

这是我们开始工作的代码:

UnderConstruction View 的 Controller 代码
    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/UnderConstruction在不同的网站?如果没有,它不会总是重定向,因为 oc永远是真的?即 - 您是否还需要为您请求的页面添加检查,以便您可以绕过重定向(如果已经转到 UnderConstruction)页?

更新

不确定检查页面名称是否是一个好主意,但这样的事情可能会奏效:

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";
}
}

请注意,我会清理它,那个例子只是给出一般的想法。

更新

如果你想使用评论中提到的角色,那么 this article from ScottGu's Blog可能有帮助。它有点复杂,但具有不引入临时代码的额外好处,因为上述解决方案将

关于asp.net - 使用 Global.asax 设置/检查 session 变量和重定向(用于用户测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com