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

标签 asp.net asp.net-mvc

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

我在 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/

相关文章:

asp.net-mvc - 在 MVC 中将 PDF(存储为 BLOB)显示为图像

asp.net - 如何在 View 中从数据库中拆分值?

c# - 在 ASP.NET MVC 中填充 DropDownList

asp.net - 将 Crystal Report 导出为文本文件 - 无 RTF 文件

c# - 可用于多个项目的变量

c# - 防止属性在 Web API 中被序列化

javascript - asp.net 更改当前 URL 中的 href 参数

c# - 访问从 ASPX/ASCX 文件生成的源代码

css - 如何根据导航栏高度计算正文填充?

c# - 最佳实践 : database class to object class in ASP. NET MVC 验证