0

I have one problem regarding to Active Directory. My project has been hosted in one server. And the active directory has been maintained in another server. Now I need the AD authientication in my application while employee login. I am totally confused while using this two different serves since I could not fetch the records of ACtive Directory; the code I have used is:

string principal = this.Context.User.Identity.Name;
string filter = string.Format("(&(ObjectClass=dev)(sAMAccountName={1}))", "dev", principal);
string domain = "SOFTWARESERVER";
string[] properties = new string[] { "fullname","mail","sn" };
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
string[] a = Context.User.Identity.Name.Split('\\');

DirectoryEntry ADEntry = new DirectoryEntry("LDAP://SOFTWARESERVER/DC=softageenapl,DC=com,DC=np");
DirectorySearcher searcher = new DirectorySearcher(ADEntry);

searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();

string Name = ADEntry.Properties["Fullname"].Value.ToString();
string displayName = directoryEntry.Properties["displayName"][0].ToString();
string firstName = directoryEntry.Properties["givenName"][0].ToString();
string lastName = directoryEntry.Properties["sn"][0].ToString();
string email = directoryEntry.Properties["mail"][0].ToString();
Kara
  • 6,115
  • 16
  • 50
  • 57
Nhuren
  • 503
  • 2
  • 10
  • 29
  • can you edit your question... it's not explicit what you really want to do, you now have 2 servers that you need to authenticate with? – balexandre Sep 07 '11 at 08:44
  • Actually it's my client Requirement getting my head. I hosted the project in one server named link server and project works fine with form authentication mode comparing username and password in database.But Client ask to use Active Directory authentication and this AD has been maintained in another server named Fileserver. Now how can I authenticate the users while logging in my system with the help of AD authentication ..... – Nhuren Sep 07 '11 at 09:37
  • If i can access the Mail addresses of the users in the AD in Fileserver for the authentication in the the projected hosted on Link server(another server), it will work out for me... so any solutions – Nhuren Sep 07 '11 at 09:43

1 Answers1

0

I know the question is from 2011. At present, I hope this code can help to somebody

(C#) Add these references:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

After that, you can use this code in your app:

PrincipalContext p = new PrincipalContext(ContextType.Domain, "IP of the server");
bool Valid = p.ValidateCredentials("User", "password");

The variable called: Valid, will show you a True value if the logIn is Ok.

Take a look in this question: Is from here, StackOverflow, and people have explained this topic with more detail ("logIn" with Microsoft Active Directory).

Community
  • 1
  • 1
Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44