C# FormsAuthentication.RedirectToLoginPage()不工作

C# FormsAuthentication.RedirectToLoginPage()不工作,c#,asp.net,C#,Asp.net,我是ASP.Net的新手。请解释一下我需要补充什么。 我在登录之前单击了此页面,以将用户带到FormsAuthentication.RedirectToLoginPage;的登录页面;。但在页面加载之后,它将转到dsRequestList。我假设它直接进入登录页面,但由于某种原因,它在dsRequestList_选择后进入登录页面。我需要做什么 protected void Page_Load(object sender, EventArgs e) { if (IsPos

我是ASP.Net的新手。请解释一下我需要补充什么。 我在登录之前单击了此页面,以将用户带到FormsAuthentication.RedirectToLoginPage;的登录页面;。但在页面加载之后,它将转到dsRequestList。我假设它直接进入登录页面,但由于某种原因,它在dsRequestList_选择后进入登录页面。我需要做什么

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将自动排除在外

不要走这条路

使用Web.config或对于MVC,使用[Authorize]属性保护页面。您不需要这种代码,也不希望它带来的每个页面都容易出错

你只需要

<configuration>
    <system.web>
       <authorization>
         <deny users="?" /> 
       </authorization>

       ...
    </system.web>

    ....
</configuration>

以保护与此web.config位于同一文件夹中的所有页面。loginform将自动排除在外

尽管答案已被接受,但我想补充一点,以澄清并回答最初的问题

执行页面上其他代码的原因是由于FormsAuthentication.RedirectToLoginPage如何工作的细节

从:

与HttpResponse.Redirect方法不同,此方法不会通过调用HttpResponse.end来结束请求。这意味着将运行RedirectToLoginPage方法调用后面的代码

不过,所说的是准确的,您不应该在页面本身或理想情况下在代码中处理身份验证,仅仅为了更改访问权限而重新编译是愚蠢的。他发布的web.config示例就足够了

另外,我将发布您的代码的修订版本,该版本不允许调用dsRequestList\u,但仍在页面级别处理身份验证,这同样是不明智的

protected void Page_Load(object sender, EventArgs e)
{
    //No need to check IsPostBack if your action is the same either way.
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        FormsAuthentication.RedirectToLoginPage();
        //Note: Using CompleteRequest() over Response.End() is significantly quicker
        //This is because it avoids raising the ThreadAbortException.
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        //So if you use CompleteRequest(), you want the return statement after.
        //Unless you wrap the rest of the Page_Load logic in an else statement, like the alternate solution below            
        return;
    }
    //All other Page_Load logic
}
由于例外情况,另一种成本更高的方法:

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        FormsAuthentication.RedirectToLoginPage();
        Response.End();
    }
    else
    {
        //All other Page_Load logic
    }
}

尽管答案已经被接受,但我想补充一点,以澄清并回答最初的问题

执行页面上其他代码的原因是由于FormsAuthentication.RedirectToLoginPage如何工作的细节

从:

与HttpResponse.Redirect方法不同,此方法不会通过调用HttpResponse.end来结束请求。这意味着将运行RedirectToLoginPage方法调用后面的代码

不过,所说的是准确的,您不应该在页面本身或理想情况下在代码中处理身份验证,仅仅为了更改访问权限而重新编译是愚蠢的。他发布的web.config示例就足够了

另外,我将发布您的代码的修订版本,该版本不允许调用dsRequestList\u,但仍在页面级别处理身份验证,这同样是不明智的

protected void Page_Load(object sender, EventArgs e)
{
    //No need to check IsPostBack if your action is the same either way.
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        FormsAuthentication.RedirectToLoginPage();
        //Note: Using CompleteRequest() over Response.End() is significantly quicker
        //This is because it avoids raising the ThreadAbortException.
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        //So if you use CompleteRequest(), you want the return statement after.
        //Unless you wrap the rest of the Page_Load logic in an else statement, like the alternate solution below            
        return;
    }
    //All other Page_Load logic
}
由于例外情况,另一种成本更高的方法:

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        FormsAuthentication.RedirectToLoginPage();
        Response.End();
    }
    else
    {
        //All other Page_Load logic
    }
}