ADSYSTEMINFO 的 VB.Net/C# 等效项是什么?在此示例中是否需要与 AD 交互?

发布于 2024-12-15 20:28:56 字数 753 浏览 3 评论 0原文

我正在查看一些旧的 ASP 代码,其中包含以下内容:

Set objDSE = GetObject("LDAP://RootDSE")
Set objSysInfo = CreateObject("adsysteminfo") 
Set objUser= Getobject("LDAP://" & Replace(objSysInfo.UserName,"/","\/"))
dtmValue = objUser.PasswordLastChanged

objMaxPwdAge = GetObject("LDAP://" & objDSE.get("DefaultNamingContext")).maxPwdAge

dblMaxPwdDays = Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart) _
                * ONE_HUNDRED_NANOSECOND / SECONDS_IN_DAY

pwdExpDate = dtmValue + dblMaxPwdDays

这是用于使用集成身份验证的 Intranet 应用程序。

ASP adsysteinfo 对象有替代品吗?我可能可以使用 System.DirectoryServices.DirectoryEntry 移植大多数 LDAP 调用,但是在 ASP.NET(VB.Net 或 C#)中是否有更好/更简单的方法来执行此操作?

有没有关于如何将 ASP 对象属性转换为 DirectoryEntry 属性的文档?

I'm looking at some old ASP code that contains the following:

Set objDSE = GetObject("LDAP://RootDSE")
Set objSysInfo = CreateObject("adsysteminfo") 
Set objUser= Getobject("LDAP://" & Replace(objSysInfo.UserName,"/","\/"))
dtmValue = objUser.PasswordLastChanged

objMaxPwdAge = GetObject("LDAP://" & objDSE.get("DefaultNamingContext")).maxPwdAge

dblMaxPwdDays = Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart) _
                * ONE_HUNDRED_NANOSECOND / SECONDS_IN_DAY

pwdExpDate = dtmValue + dblMaxPwdDays

This is for an intranet app that uses integrated authentication.

Is there a replacement for the ASP adsysteinfo object? I can probably port most of the LDAP calls using System.DirectoryServices.DirectoryEntry, but is there a better/easier way to do this in ASP.NET (VB.Net or C#)?

Is there any documentation for how to convert the ASP object properties to DirectoryEntry properies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2024-12-22 20:28:56

在您的示例代码中,adsysteminfo 仅用于检索当前用户名。在 ASP.Net 应用程序中,您可以通过以下两种方式之一获取此信息,具体取决于您的配置:

1) 如果您模拟每个用户,那么您应该能够使用:

Return System.Security.Principal.WindowsIdentity.GetCurrent().Name

2) 如果您不是模拟用户,而是您确实有集成身份验证作为网站的唯一安全机制,那么您可以按如下方式获取用户的域名:

Return Request.ServerVariables("logon_user")

至于其他 LDAP 调用,System.DirectoryServices 肯定是您想要使用的。下面是我们如何连接到 AD 并开始搜索用户的示例:

Private m_Searcher As DirectorySearcher
Private m_sNamingContext As String

    Dim theRootEntry As DirectoryEntry
    Dim theEntry As DirectoryEntry
    Dim theNamingContext As Object

    ' First, fetch any information that we need from the database
    If Not GetConfigurationInfoFromDB() Then
        Return False
    End If

    ' Obtain the domain root entry
    theRootEntry = New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
    ' Verify that we retrieved it correctly and raise an error if we did not
    If theRootEntry Is Nothing Then
        Throw New Exception("A directory services entry for the LDAP RootDSE could not be created.")
    End If

    ' Get the root naming context
    theNamingContext = theRootEntry.Properties("rootDomainNamingContext").Value
    ' Verify that we retrieved it correctly and raise an error if we did not
    If (theNamingContext Is Nothing) OrElse (theNamingContext.ToString().Length = 0) Then
        Throw New Exception("The root domain naming context property could not be retrieved from the LDAP directory services")
    Else
        m_sNamingContext = theNamingContext.ToString()
    End If

    ' And create a new directory entry for the root naming context
    theEntry = New DirectoryEntry("LDAP://" & m_sNamingContext)
    ' Verify that we retrieved it correctly and raise an error if we did not
    If theEntry Is Nothing Then
        Throw New Exception("A directory entry object could not be created for LDAP://" & m_sNamingContext)
    End If

    ' Now we configure what we are looking for from Active Directory

    ' Start with a new searcher for the root domain
    m_Searcher = New DirectorySearcher(theEntry)

然后我们可以使用 m_Searcher 开始从 AD 中提取我们需要的内容(这只是使用目录服务的一个示例)。

In your example code, adsysteminfo is just use to retrieve the current user name. In an ASP.Net application, you can get this one of two ways, depending on your configuration:

1) If you are impersonating each user, then you should be able to use:

Return System.Security.Principal.WindowsIdentity.GetCurrent().Name

2) If you are not impersonating the user, but you do have integrated authentication as the only security mechanism for the web site, then you can get the user's domain name as follows:

Return Request.ServerVariables("logon_user")

As for the other LDAP call, System.DirectoryServices is definitely what you want to use. Here is a sample of how we get hooked into AD to start searching for users:

Private m_Searcher As DirectorySearcher
Private m_sNamingContext As String

    Dim theRootEntry As DirectoryEntry
    Dim theEntry As DirectoryEntry
    Dim theNamingContext As Object

    ' First, fetch any information that we need from the database
    If Not GetConfigurationInfoFromDB() Then
        Return False
    End If

    ' Obtain the domain root entry
    theRootEntry = New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
    ' Verify that we retrieved it correctly and raise an error if we did not
    If theRootEntry Is Nothing Then
        Throw New Exception("A directory services entry for the LDAP RootDSE could not be created.")
    End If

    ' Get the root naming context
    theNamingContext = theRootEntry.Properties("rootDomainNamingContext").Value
    ' Verify that we retrieved it correctly and raise an error if we did not
    If (theNamingContext Is Nothing) OrElse (theNamingContext.ToString().Length = 0) Then
        Throw New Exception("The root domain naming context property could not be retrieved from the LDAP directory services")
    Else
        m_sNamingContext = theNamingContext.ToString()
    End If

    ' And create a new directory entry for the root naming context
    theEntry = New DirectoryEntry("LDAP://" & m_sNamingContext)
    ' Verify that we retrieved it correctly and raise an error if we did not
    If theEntry Is Nothing Then
        Throw New Exception("A directory entry object could not be created for LDAP://" & m_sNamingContext)
    End If

    ' Now we configure what we are looking for from Active Directory

    ' Start with a new searcher for the root domain
    m_Searcher = New DirectorySearcher(theEntry)

Then we can use m_Searcher to start extracting what we need from AD (this is just one example for using directory services).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文