c# - FormsAuthentication.RedirectToLoginPage() 不工作

标签 c# asp.net

我是 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/

相关文章:

c# - Wp插入数据库异常

C# 串行数据丢失?

html - 使用 ASP.NET 将 HTML 表导出到 Excel

c# - 如何使用 Linq Lambda 检索 XML?

C# 在读取异步时等待 shell 命令返回

c# - 如何打包 Visual Studio aws 无服务器项目?

c# - 如何在单个 Linq 表达式中混合 monadic 构造?

c# - ASP.Net OAuth 授权服务器 : Add an array as additional response parameter

asp.net - 在网站的 SQL Server 中存储文件(图片)的优缺点

asp.net - 带有不可见文本框的 Ajax Calendarextender 位置