<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Check if user existing/valid in Active Directory</Title>
      <Shortcut>Checkifuserexisting/validinActiveDirectory</Shortcut>
      <Description>Check if user existing/valid in Active Directory [VB.NET]</Description>
      <Author>Raphael Okelola</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=1a9d7f4d-d85f-4bff-924d-c3b4b377c321</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="VB"><![CDATA[Private Function UserExistsInAD(ByVal domainName As String, ByVal userName As String) As Boolean 
    If domainName Is Nothing Then 
        Throw New ArgumentNullException("domainName", "Domain Name can not be null") 
    End If 
    If userName Is Nothing Then 
        Throw New ArgumentNullException("userName", "User Name can not be null.") 
    End If 
    Try 
        Dim search As New DirectorySearcher(New DirectoryEntry([String].Format("LDAP://{0}", domainName), Nothing, Nothing)) 
        search.Filter = [String].Format("(|(cn={0})(SAMAccountName={0}))", userName) 
        search.PropertiesToLoad.Add("cn") 
        search.PropertiesToLoad.Add("samaccountname") 
        search.PropertiesToLoad.Add("givenname") 
        search.PropertiesToLoad.Add("sn") 
        search.PropertiesToLoad.Add("memberOf") 
        Dim result As SearchResult = search.FindOne() 
        If result Is Nothing Then 
            Return False 
        Else 
            Return True 
        End If 
    Catch e As Exception 
        Throw e 
    End Try 
End Function 
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>