[英]asp.net login system via c#
我有四個頁面login.aspx
、 main.aspx
、 Sub.aspx
、 final.aspx
。 最終頁面需要訪問我使用 forms 身份驗證提供的身份驗證。通過login.aspx
登錄它重定向到final.aspx
,因為defaultUrl
設置為final.aspx
。目前我從main.aspx
到sub.aspx
到final.aspx
在登錄final.aspx
后重定向到login.aspx
。 我想要的是,如果我從login.aspx
開始,它應該重定向到main.aspx
到sub.aspx
到final.aspx
,從sub.aspx
跳轉到final.aspx
不應該重定向到已經登錄的登錄。但直接訪問final.aspx
應始終重定向到login.aspx
登錄代碼
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1 .Text =="abc" && TextBox2 .Text =="xyz")
{
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
}
else
{
Response.Write("w");
}
}
最后一頁代碼
protected void Page_Load(object sender, EventArgs e)
{
if (User .Identity .IsAuthenticated )
{
Response.Write("welcome");
}
else
{
FormsAuthentication.RedirectToLoginPage();
}
}
web 配置
<authentication mode ="Forms">
<forms name ="abc" loginUrl ="login.aspx" defaultUrl="final.aspx"/>
</authentication>
我會這樣做:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
Response.Write("welcome");
}
else
{
Response.Redirect("/login.aspx");
}
}
我可能會首先顯示一條消息“未授權。請登錄”,其中包含指向登錄頁面的鏈接。 而不是重定向。
希望這可以幫助。
對於這種情況,我建議您更好地使用 Session 來處理 URL 重定向和身份驗證。
嘗試這個
要使用 Session,您需要在 class 中繼承“頁面”:
public class Class1 : Page
子頁面
aspx:
<a id="gotofinal" href="#" OnClick="GoToFinal_Click">Go To Final</a>
aspx.cs:
protected void GoToFinal_Click(object sender, EventArgs e)
{
Session["GoToFinal"] = "Y";
Response.Redirect("final.aspx");
}
最后一頁:
protected void Page_Load(object sender, EventArgs e) {
if ((string)Session[IsAuthenticated] == "Y")
{
Session["GoToFinal"] = "";
Response.Write("welcome");
}
else
{
Response.Redirect("login.aspx");
}
}
登錄頁面:
protected void Page_Load(object sender, EventArgs e) {
Session["IsAuthenticated"] = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "abc" && TextBox2.Text == "xyz")
{
Session["IsAuthenticated"] = "Y";
if (!String.IsNullOrEmpty((string)Session["GoToFinal"]) && (string)Session["GoToFinal"] == "Y")
{
Response.Redirect("final.aspx");
}
else
{
Response.Redirect("main.aspx");
}
}
else
{
Response.Write("Login Failed");
}
}
您還需要在 final.aspx 中制作注銷按鈕以清除 Session[IsAuthenticated]
Session["IsAuthenticated"] = "";
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.