企业内网用户应用级Windows身份验证(ASP.. NET和VB)



我的公司使用ActiveDirectory, IT部门自然希望保持对它的控制,而不是将控制权交给其他用户。我正在开发一个ASP。. NET应用程序(仅供内部使用)使用SQL Server 2008数据库。

我的问题是,我如何BEST使用。net名称空间和SQL Server来管理对应用程序(或可能的DB)级别组织内应用程序的访问?我希望根据ActiveDirectory提供的网络用户名对用户进行授权。

顺便说一句,我还想访问他们的广告联系信息。

根据我的理解,我可以在系统中使用ActiveDirectoryMembershipProvider类或域服务。DirectoryServices名称空间。LDAP显然是另一种可能。我很难理解这一切,更不用说哪个是最好的选择以及如何实现它了。谁能给我提供一些方向和可能一些简单的示例代码?

UPDATE:对不起,我忘了说我正在使用VB。. NET作为我的代码源,因为它是公司标准。

感谢!div;)

这是一个快速的代码片段,它开始了我与Active Directory和ASP.NET的永无休止的旅程。这是从一个小的测试页面,我放了一个文本框,插入一个局域网ID,它返回所有可用的AD字段。

    Protected Sub btnSearchUserDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchUserDetails.Click
    Dim Entry1 As New System.DirectoryServices.DirectoryEntry("LDAP://DC=ent,DC=foo,DC=bar,DC=corp", "yourID", "yourPassword")
    Dim search As New DirectorySearcher(Entry1)
    search.Filter = [String].Format("(SAMAccountName={0})", txtSearchUser.Text)
    Dim result As SearchResult = search.FindOne()
    Dim user As DirectoryEntry = result.GetDirectoryEntry()
    PrintDirectoryEntryProperties(user)
    End Sub

Private Sub PrintDirectoryEntryProperties(ByVal entry As System.DirectoryServices.DirectoryEntry)

    'loop through all the properties and get the key for each prop

    lblPropList.Text = "<table>"
    For Each Key As String In entry.Properties.PropertyNames
        Dim sPropertyValues As String = [String].Empty
        'now loop through all the values in the property;
        'can be a multi-value property
        For Each Value As Object In entry.Properties(Key)
            sPropertyValues += Convert.ToString(Value) + ";<br>"
        Next
        'cut off the separator at the end of the value list
        sPropertyValues = sPropertyValues.Substring(0, sPropertyValues.Length - 5)
        'now add the property info to the property list
            lblPropList.Text += "<tr><td>" & Key & "</td><td>" & sPropertyValues & "</td></tr>"
    Next
    lblPropList.Text += "</table>"
End Sub

获取当前认证用户的AD登录ID Request.ServerVariables("LOGON_USER")Httpcontext.Current.User.Identity.Name将成为您的朋友。请记住,如果使用Allow Anonymous安全性访问ASP页面,则不会填充LOGON_USER变量。

我要回顾一下我的一些笔记,试着找到一些我用过的对我最有帮助的资源。我可以脱口而出地告诉你,我每天都在使用《。net开发人员目录服务编程指南》这本书。

我后来发现,Windows身份验证模式在内部网环境中工作得很好。在开发时,我决定在我的IIS配置中启用此模式,并让每个页面通过其网络登录自动识别用户并指向他们(或允许/禁止访问适当的页面)。请记住,我的解决方案只适用于内部网/如果你在域控制器上。

这是相关的Web.config

<system.web>
    <authentication mode="Windows" />
    <identity impersonate="false" />
</system.web>

我认为最简单的方法是在数据库中查找用户名并找到与之相关的角色/组,然后评估该角色是否应该在VB代码中允许他们访问所请求的页面。下面是获取用户网络用户名的方法:

' While logged into your intranet will return "DOMAINusername"
Dim username As String = Page.User.Identity.Name

下面是一些VB示例。NET代码,可用于身份验证(自动)和允许访问给定页面(不完全是我使用的代码,只是一个示例)。

Dim role As String
Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
    Dim query As New SqlCommand("select top (1) Role from Users where Username like '" + username + "'", con)
    con.Open()
    role = query.ExecuteScalar().ToString
End Using
If StrComp(role, "Admin") = 0 Then
    welcomeLabel.Text = "Welcome! You may enter"
Else
    HttpContext.Current.Server.Transfer("/Kick.aspx")
End If

我希望有些人觉得这有用。我花了无数个小时才想出了一个几乎和这个一样的解决方案。

欢呼;)

使用"在数据库中查找用户名并找到与其相关的角色/组"的公认答案在我看来是没有抓住重点的。

解决方案是在Visual Studio中检查NTLM身份验证复选框(使用版本2012是在项目的属性,Web,服务器;其他版本应该类似)。

最新更新