[英]Redirect after login with C# to HTML page. But one should not open that HTML just by copying that URL
我是ASP.Net和C#的新手。 我想用C#執行登錄,然后再重定向到HTML文件。 該HTML是SuperGIS Server的起始頁面。
我正在使用這樣的東西:
<%@ Page Language="C#" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnlogin_Click(object sender, EventArgs e)
{
if(some code to check username and password exist in DB)
Response.Redirect("http://localhost/AddMarker/MapEditor.htm");
else
Response.Redirect("http://localhost/AddMarker/Login.aspx");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Sign In<br />
<br />
<asp:Label ID="lblUsername" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate = "txtUsername"
ErrorMessage="This Feild is Mendetory"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnlogin" runat="server" Text="Login" onclick="btnlogin_Click"
Width="47px" />
<br />
</div>
</form>
<a href="forgetpass.aspx">Forgt Password</a><br>
<a href="Create.aspx">New User</a>
</body>
</html>
此代碼將重定向到所需的目的地。 但那時url將是
->“ http://localhost/AddMarker/MapEditor.htm”
這是由於代碼:Response.Redirect(“ http://localhost/AddMarker/MapEditor.htm”);
因此,任何人都可以復制此URL並訪問此服務而無需身份驗證。
因此,我可以在不提供正確的ID和密碼的情況下創建一些不能使用的動態URL。
請幫我。 提前致謝。
您可以使用會話狀態存儲用戶是否通過身份驗證。
if(some code to check username and password exist in DB)
{
HttpContext.Current.Session("UserIsAuthenticated") = True
Response.Redirect("http://localhost/AddMarker/MapEditor.htm");
}
您可以在web.config
設置會話時間,以便僅在一定時間內保持身份驗證。 然后,在需要認證的頁面的加載事件中,使用以下命令:
if (HttpContext.Current.Session("UserIsAuthenticated") == null)
//User needs to login
Response.Redirect("Login.aspx")
else if(!HttpContext.Current.Session("UserIsAuthenticated"))
//User needs to login
Response.Redirect("Login.aspx")
您也可以使用cookie來執行此操作,但由於新的cookie立法,我建議不要在可能的情況下執行此操作。
我認為最好的方法是在MapEditor.htm中使用ASP .Net來檢查訪問權限。 驗證用戶身份后,您將在會話中存儲您的標志。 然后,您總是在MapEditor中檢查此標志。 ASPX
您應該將登錄狀態檢查放在Global.asax中。 使用諸如Application_BeginRequest之類的事件之一來查看用戶是否已登錄,如果沒有,則重定向。 您將需要確保用戶嘗試上拉的頁面不是登錄頁面,盡管否則它將無法正常工作。
更新 :這是我在母版頁中使用的一些代碼,效果很好。 如果您不使用母版頁,則將其放置在正確的位置即可使用Global.asax。
請注意,由於站點使用Windows身份驗證,因此該代碼適用於檢查用戶是否在特定的域組中。 您可以換出驗證用戶所需的任何方法。 另外,在我的NoAccess頁面中,AccessException布爾值也設置為true,以便實際顯示它。 最后,檢查被緩存,因此我們不必繼續檢查它。
public bool AccessException;
protected void Page_Load(object sender, EventArgs e)
{
UpdateNavigation();
if (!HasAccess() && !AccessException)
{
Response.Redirect("~/NoAccess.aspx", true);
}
}
/// <summary>
/// Check domain group membership to make sure the user is allowed to use the web site; redirect if not.
/// </summary>
/// <returns>True if the user has access to the site</returns>
private bool HasAccess()
{
if (Page.Cache["hasAccess"] == null)
{
List<string> userDomainGroups = Common.GetUserDomainGroups(Request.LogonUserIdentity);
string permittedGroups = ConfigurationManager.AppSettings["AllowedGroups"];
Page.Cache["hasAccess"] = userDomainGroups.Where(permittedGroups.Contains).Count() != 0; ;
}
return (bool)Page.Cache["hasAccess"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.