我是 ASP.Net 的新手。请解释我需要添加的内容。 我在登录前单击此页面,通过“FormsAuthentication.RedirectToLoginPage();”将用户带到登录页面。但是在 Page_Load 之后,它转到“dsRequestList_Selecting”。我假设它直接进入登录页面,但这里有一些原因它在 dsRequestList_Selecting() 之后进入登录页面。我需要做什么?
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
}
else
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
}
}
protected void dsRequestList_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
//Selecting
}
最佳答案
别往这边走。
使用 Web.config(或者,对于 MVC,[Authorize]
属性)来保护页面。您不需要这种代码,也不希望它(在每个页面中)容易出现错误。
你只需要
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
...
</system.web>
....
</configuration>
在与此 web.config 相同的文件夹中保护您的所有页面。 loginform 自动排除。
关于c# - FormsAuthentication.RedirectToLoginPage() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506780/