Tôi muốn thêm bảo mật tạm thời rất đơn giản vào trang web của mình.Sử dụng Global.asax để đặt/kiểm tra biến phiên và chuyển hướng (để kiểm tra người dùng)
Tôi đã tạo trang tại Trang chủ/UnderConstruction nơi mọi người kiểm tra trang web có thể nhập mật khẩu mã hóa cứng, sau đó sẽ đặt biến phiên "underconstruction" thành "false".
Đây là những gì tôi có cho đến nay, nhưng nó kết quả trong quá nhiều chuyển hướng:
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");
}
}
}
Đây có phải là gần với những gì tôi cần phải làm gì?
Đây là mã chúng tôi đã nhận làm việc:
điều khiển Mã cho nhật Xem
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";
}
}
Bạn đã thử nó chưa? Đó là nơi tôi sẽ bắt đầu – Tad
Có, nhưng như tôi đã nói, nó kết quả trong một vòng lặp vô tận. Tôi không chắc chắn làm thế nào để sửa chữa nó. – Dave
Chỉ là một suy nghĩ, nhưng bạn có thể thay đổi câu lệnh if của bạn thành 'if (oc ==" true ")' khi bạn không cắt tỉa hoặc hạ thấp nó để so sánh không? – Mightymuke