【问题标题】:Forms authentication not authenticating表单身份验证未通过身份验证
【发布时间】:2013-05-27 10:36:41
【问题描述】:

我已经根据我从 MCTS 自定进度培训套件中了解的内容设置了以下表单身份验证,用于 MCTS 考试 70-562,但它没有进行身份验证...

    <authentication mode="Forms">
        <forms name="ortund" loginUrl="~/Login.aspx" timeout="30" slidingExpiration="true" />
    </authentication>
</system.web>

<location path="Members">
    <system.web>
        <authorization>
            <deny users="?" />
        </authorization>
    </system.web>
</location>

基本上,它需要允许未经身份验证的用户访问网站的所有区域,~/Members/ 以及其中的所有文件和文件夹除外。

目前它所做的只是不断地重定向回登录页面......我在这里缺少一个概念吗?我不明白我做错了什么。

这是登录代码:

Protected Sub lnkLogin_Click(sender As Object, e As System.EventArgs) Handles lnkLogin.Click
    Dim db As New Database

    ' gets data from the database with the supplied credentials
    ' if true, the user exists, proceed to log in
    If db.Login(txtEmail.Text, txtPassword.Text, "ortund") Then
        FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, True)

        If Not String.IsNullOrEmpty(Request.Params("ReturnUrl")) Then
            Response.Redirect(Request.Params("ReturnUrl"))
        Else
            Response.Redirect("~/Members/Default.aspx")
            'Response.Redirect("~/AboutUs.aspx")
        End If
    Else
        lblerr.Text = "Invalid username or password"
    End If
End Sub

Members/Default.aspx 的 Page_Load:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        If Context.User.Identity.IsAuthenticated Then
            loadUserPage()
        End If
    End If
End Sub

【问题讨论】:

  • 据我了解,这应该可行,允许 30 分钟的超时时间在用户执行操作时重置...

标签: asp.net vb.net web-config forms-authentication


【解决方案1】:

Aaaaand 在 SO、IRC、Facebook 和 Skype 上的 2 个令人发狂的甜蜜内容令人发狂之后......这是我修复这个问题的方法:

我创建了一个测试应用程序只是为了确保我的概念是正确的。该测试与上面的不同之处仅在于它使用 web.config 文件中指定的凭据进行登录,而实际项目查询数据库以获取用户凭据。

您可以下载我的测试代码here(它使用 .NET 4,但您也可以将 web.config 文件中的相同身份验证元素应用到更早的框架)。

因为这个测试用例有效,我可以确定问题不在代码中,这意味着这是一个设置问题。

解决方案
在这种情况下,修复很简单。我从 IIS 中删除了虚拟应用程序并使用 .NET 4.0 Classic AppPool 重新创建它。

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多