非常に簡単な一時的なセキュリティを自分のサイトに追加したいと思います。Global.asaxを使用してセッション変数を設定/確認し、リダイレクトする(ユーザーテスト用)
サイトをテストしているユーザーがハードコードされたパスワードを入力できる「Underconstruction」セッション変数を「false」に設定するページをHome/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");
}
}
}
は私が行う必要があるだろうかと、この近いですか?
ここでは、動作するようになったコードです:
コントローラコードUnderConstruction Viewの
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";
}
}
試しましたか?それは私が始めるところです – Tad
はい、私が言ったように、それは無限ループに終わります。私はそれを修正する方法がわかりません。 – Dave
ちょっと考えましたが、if文を 'if(oc ==" true ")'に変更することはできますか? – Mightymuke