开发者

Active Directory in Asp.net C#

开发者 https://www.devze.com 2023-04-03 08:31 出处:网络
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 appl

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(开发者_Python百科"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();


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消