2011-11-16 34 views
0

我看,它包含以下一些老的ASP代碼:什麼是ADSYSTEMINFO的VB.Net/C#等價物,是否需要在此示例中與AD交互?

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 

這對於使用集成身份驗證的應用程序內聯網。

是否有替代ASP adsysteinfo對象?我可以使用System.DirectoryServices.DirectoryEntry移植大部分LDAP調用,但是在ASP.NET(VB.Net或C#)中有更好/更簡單的方法嗎?

是否有任何有關如何將ASP對象屬性轉換爲DirectoryEntry特性的文檔?

+1

你在使用傳統的ASP或ASP.net嗎? – Dee

+0

當前的代碼是經典的asp,我需要在asp.net中複製功能。 – chris

回答

1

在您的示例代碼中,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需要(這是使用目錄服務只是一個例子)。

+0

那麼如何使用m_Searcher搜索maxPwdAge? – chris

相關問題