Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1229
  • Last Modified:

Active Directory Login failed when using C#

Hell experts!
I'm facing a weird problem when connection to AD using C#.
When I run the C# project, it fails and gives me an error saying that the user or password may be invalid.
I converted the code to VB.Net and it works perfectly.
The problem is ocurring when I call the FindOne method.
What could be causing this problem?
I've attached both c# and vb.net code to this message.

Please, help.
C# code:
 
            this.AD = new DirectoryEntry(loginPath, loginUsername, loginPassword);
            var filter = "(&(objectCategory=Person) (sAMAccountName=" + username + "))";
            var diretorySearcher = new DirectorySearcher(this.AD);
            SearchResult searchResult = null;
            diretorySearcher.SearchRoot = this.AD;
            diretorySearcher.SearchScope = SearchScope.Subtree;
            diretorySearcher.Filter = filter;
            searchResult = diretorySearcher.FindOne();
            DirectoryEntry user = searchResult.GetDirectoryEntry();
 
VB.Net code:
            Me.AD = New DirectoryEntry(loginPath, loginUsername, loginPassword)
            String filter = "(&(objectCategory=Person) (sAMAccountName=" & username & "))"
            DirectorySearcher diretorySearcher = New DirectorySearcher(Me.AD)
            SearchResult searchResult = Nothing
            diretorySearcher.SearchRoot = Me.AD
            diretorySearcher.SearchScope = SearchScope.Subtree
            diretorySearcher.Filter = filter
            searchResult = diretorySearcher.FindOne()
            DirectoryEntry user = searchResult.GetDirectoryEntry()

Open in new window

0
andrepires
Asked:
andrepires
  • 3
  • 2
1 Solution
 
Gautham JanardhanCommented:
after the first line try this
object Native = this.AD.NativeObject;

Open in new window

0
 
andrepiresAuthor Commented:
Ok, but what should I do with that Native object?
0
 
Gautham JanardhanCommented:
nothing, that line basically tries to validate the entry against AD and makes connection with AD
0
 
andrepiresAuthor Commented:
Ok... I'll try that and let you know.
Thanks.
0
 
andrepiresAuthor Commented:
Hello.
The solution did not work.
I was forced to compile an assembly created with VB.Net and consume it in my application.
Thanks for helping!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now