我在ASP.NET上使用表單身份驗證。如果我試圖通過複製查詢字符串並將其粘貼到瀏覽器來訪問頁面,它允許我訪問該頁面。如何防止URL輸入並將用戶重定向到登錄頁面?
這怎麼能防止?我希望用戶始終必須登錄。
我在ASP.NET上使用表單身份驗證。如果我試圖通過複製查詢字符串並將其粘貼到瀏覽器來訪問頁面,它允許我訪問該頁面。如何防止URL輸入並將用戶重定向到登錄頁面?
這怎麼能防止?我希望用戶始終必須登錄。
您可以通過使用<location>
元素來限制對某些頁面的訪問。因此,例如,限制訪問的子文件夾admin
:
<system.web>
<!-- enable Forms authentication -->
<authentication mode="Forms">
<forms
name="MyAuth"
loginUrl="login.aspx"
protection="All"
path="/"
/>
</authentication>
</system.web>
<!-- restrict access to the admin subfolder
and allow only authenticated users -->
<location path="admin">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
您必須設置身份驗證模式在你的web.config
<authentication mode="Forms">
<forms name="Authen" protection="All" timeout="60" loginUrl="login.aspx"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
您應該添加在web.config文件中類似的東西:
<authorization>
<allow users="user1, user2"/>
<deny users=」?」/>
</authorization>
這應該解決問題。請參閱:http://support.microsoft.com/kb/815151
除了在的web.config文件驗證配置,您還可以使用在Global.asax 在session_start(...)方法來檢查用戶的新會話,也一定要修改此外
public class Global:System.Web.HttpApplication
{
protected void Session_Start(object sender, EventArgs e)
{
if(Session.IsNewSession)
{
if (Request.Headers["Cookie"] != null)
{
if (Request.Headers["Cookie"].IndexOf("Web_App_Login_Cookie", StringComparison.OrdinalIgnoreCase) >= 0)
{
FormsAuthentication.SignOut();
Context.User = null;
Response.Redirect("~/logOn.aspx");
}
}
}
}
}
,如果存儲用戶會話信息在某些類可以覆蓋的OnInit(...)方法在某些:會話cookie,如果是空,你應該將用戶重定向到登錄頁面基類以確保用戶已經存在於某個自定義會話集合中,如果n ot再次你應該重定向到登錄頁面。
public class SessionBasePage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
UserSession = HttpContext.Current.GetUserSession();
if (UserSession != null)
{
LoggedUserInfo = HttpContext.Current.GetLoggedUserInfo();
HttpContext.Current.UpdateLoggedUserInfo();
}
else { Response.Redirect("~/logOn.aspx", true); }
}
}
}
+1打我。您可以通過添加指向loginUrl Property – 2010-10-26 17:20:31
的鏈接來增強您的答案,當前它在授權上顯示。 –
kalls
2010-10-26 17:22:42