Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Authenticate a User Against the Directory
- private bool Authenticate(string userName,string password, string domain)
- {
- if (userName == "" || password == "")
- {
- return false;
- }
- bool authentic = false;
- try
- {
- DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,userName, password);
- object nativeObject = entry.NativeObject;
- authentic = true;
- }
- catch (DirectoryServicesCOMException) { }
- return authentic;
- }
- private bool Authenticate(string userName,string password, string domain, string group)
- bool IsInGroup(string user, string group)
- {
- using (var identity = new WindowsIdentity(user))
- {
- var principal = new WindowsPrincipal(identity);
- return principal.IsInRole(group);
- }
- }
- public bool AuthenticateGroup(string userName, string password, string domain, string group)
- {
- if (userName == "" || password == "")
- {
- return false;
- }
- try
- {
- DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password);
- DirectorySearcher mySearcher = new DirectorySearcher(entry);
- mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
- SearchResult result = mySearcher.FindOne();
- foreach (string GroupPath in result.Properties["memberOf"])
- {
- if (GroupPath.Contains(group))
- {
- return true;
- }
- }
- }
- catch (DirectoryServicesCOMException)
- {
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement