【问题标题】:how to tell why integrated windows authentication fails in asp.net如何判断为什么在 asp.net 中集成 Windows 身份验证失败
【发布时间】:2011-01-01 09:06:58
【问题描述】:

在我们的 asp.net Intranet 应用程序中,我们使用 Windows 身份验证来验证用户身份。

我们最近收到了一个请求,要求用户说明他们无法登录的原因。例如,告诉用户他们无法登录,因为他们的密码已过期,而他们无法登录,因为他们的帐户被锁定。

当帐户被锁定或密码过期时,用户无法登录应用程序。 IIS 将在 3 次登录尝试后拒绝访问并将用户重定向到拒绝访问 (401) 页面。由于 IIS 身份验证失败时用户名没有传递给 Web 应用程序,因此我们无法检查帐户是否被锁定或密码是否已过期。

关于如何获取此信息的任何建议? 我们是否将不得不转向使用 AD 提供商进行表单身份验证?

【问题讨论】:

    标签: active-directory asp.net-membership


    【解决方案1】:

    解决这个问题的简单方法是改用表单身份验证。但是,我知道您不想听到这种情况,而且这是不允许的或可行的解决方案,您的下一个选择是:

    查看 System.DirectoryServices

    下面我只是粘贴一些你可以玩的快速代码。注意如何确定用户是否被锁定。这是 vb.net,但可以轻松更改为 C#。

      Try
                Dim dirEntry As DirectoryEntry
                         dirEntry = New DirectoryEntry("LDAP://yourDomainInfoHere/OU=Users,OU=YourDomain,OU=YourOU,OU=CORP,DC=YourDC,DC=com", "ExecuateAsUser", "Password")
    
                Dim entries As DirectoryEntries = dirEntry.Children
                ' Set login name and full name. 
                Dim newUser As DirectoryEntry = entries.Add("CN=JONNY BOY", "User")
    
                newUser.Properties("sAMAccountName").Add("jboy")
                newUser.CommitChanges()
                newUser.Invoke("SetPassword", "hi2343145gfdtgwdt")
                Dim flags As Integer
    
                flags = CInt(newUser.Properties("userAccountControl").Value)
    
                'enable user below
                newUser.Properties("userAccountControl").Value = flags And Not &H2
    
                'disable user below
                newUser.Properties("userAccountControl").Value = flags Or &H1
    
    
                'lockout property
                Dim l As Long
                l = CType(newUser.Properties("lockoutTime").Value, Long)
    
                If l <> 0 Then
                    'account is locked out
    
                    'so how do we unlock it?
                    'we unlock it by setting it to 0
                    newUser.Properties("lockoutTime").Value = 0
                Else
                    'account is 0 it is NOT locked out
    
                End If
    
                newUser.CommitChanges()
    
                Dim j As DirectoryEntry = entries.Find("CN=JONNY BOY", "User")
                j.Properties("mail").Value = "jon@yahoo.com"
                j.CommitChanges()
            Catch ex As Exception
                Throw ex
            End Try
    

    【讨论】:

    • 如您所说,唯一真正的解决方案是将身份验证从 Windows 更改为表单,并实现我们自己的登录页面和身份验证方法。我们最终创建了一个“故障排除页面”,认证失败的用户可以在其中重新输入他们的用户名/密码。然后我们获取该用户名并进行 AD 查询以确定身份验证失败的原因。它涉及多个凭据条目,但它符合业务要求(和预算),因此每个人都很高兴。
    • 很好,我很高兴你让它为你工作而高兴。也感谢您接受的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2011-02-03
    • 1970-01-01
    • 2011-02-22
    • 2016-11-24
    • 2015-04-24
    • 2021-08-03
    相关资源
    最近更新 更多