Posts Tagged asp.net

IsNull em VB.NET

Estava aqui de volta de um site feito em ASP.NET (VB.NET) e estava-me a fazer falta o IsNull do C#.
Solução? Utilizar o “Is Nothing:)

Exemplo:

If (Not Request.QueryString("accao") Is Nothing) Then
   Response.Write(" A variavel 'accao' = " & Request.QueryString("accao"))
End If

Tags: ,

ASP.NET – Ler nome do Utilizador na Active Directory

Noutro dia precisei de ir ler o nome dos utilizadores na Active Directory.
Fica o resultado:

VB.NET
Imports System.DirectoryServices

Public Class teste
   Function GetLdapUserName(ByVal login As String) As String

    Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://servidor:porta/DC=xpto,DC=com", "user", "password")
    Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
    Dim oresult As SearchResultCollection
    Dim result As SearchResult

    osearcher.Filter = "(&(cn=" + login + "))" ' filtro de procura
    osearcher.PropertiesToLoad.Add("displayName") ' propriedade a ler
    oresult = osearcher.FindAll()

    For Each result In oresult
       If Not result.GetDirectoryEntry.Properties("displayName").Value Is Nothing Then
          Return (result.GetDirectoryEntry.Properties("displayName").Value)
       End If
    Next

    Return "Não foi possivel obter o seu nome..."

 End Function
End Class

Outras propriedades que podem ser lidas na Active Directory:

  osearcher.PropertiesToLoad.Add("ou") ' organizational unit
  osearcher.PropertiesToLoad.Add("userPrincipalName") ' login name
  osearcher.PropertiesToLoad.Add("distinguishedName") ' distinguised name
  osearcher.PropertiesToLoad.Add("name") ' full name
  osearcher.PropertiesToLoad.Add("givenname") ' firstname
  osearcher.PropertiesToLoad.Add("sn") ' lastname
  osearcher.PropertiesToLoad.Add("mail") ' mail

Tags: , ,