Как подключиться к домену Active Directory и проверить, в каких группах текущий пользователь - C#
Формулировка задачи:
Снова привет!
Не подскажете как на .НЕТ подключиться к домену Active Directory, в котором я авторизировался при входе в Windows и проверить там в каких группах мой текущий пользователь?
Желательно, чтобы именно пользователь не вбивал внутри монго приложения снова логин, пароль и домен, а автоматически запросить эту информацию у Windows, ну а проверить в каких группах пользак я думаю и сам смогу если что разобраться, на PHP даже такое делал.
Решение задачи: «Как подключиться к домену Active Directory и проверить, в каких группах текущий пользователь»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
namespace ADTest
{
class Program
{
static void Main(string[] args)
{
try
{
string sAMAccountName;
string sn;
string givenName;
string displayName;
string l;
string streetAddress;
string co;
string mail;
string wWWHomePage;
string mobile;
string homePhone;
string employeeID;
string title;
string company;
string department;
string phone;
string ou;
DirectoryEntry de = new DirectoryEntry("LDAP://DC=Home,DC=local");
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.SearchScope = SearchScope.Subtree;
for (int i = 97; i < 97 + 26; i++)
{
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + Convert.ToChar(i) + "*))";
SearchResultCollection result = deSearch.FindAll();
for (int x = 0; x < result.Count; x++)
{
sAMAccountName = Convert.ToBoolean(result[x].Properties["sAMAccountName"].Count > 0) ? result[x].Properties["sAMAccountName"][0].ToString() : "";
sn = Convert.ToBoolean(result[x].Properties["sn"].Count > 0) ? result[x].Properties["sn"][0].ToString() : "";
givenName = Convert.ToBoolean(result[x].Properties["givenName"].Count > 0) ? result[x].Properties["givenName"][0].ToString() : "";
displayName = Convert.ToBoolean(result[x].Properties["displayName"].Count > 0) ? result[x].Properties["displayName"][0].ToString() : "";
l = Convert.ToBoolean(result[x].Properties["l"].Count > 0) ? result[x].Properties["l"][0].ToString() : "";
streetAddress = Convert.ToBoolean(result[x].Properties["streetAddress"].Count > 0) ? result[x].Properties["streetAddress"][0].ToString() : "";
co = Convert.ToBoolean(result[x].Properties["co"].Count > 0) ? result[x].Properties["co"][0].ToString() : "";
mail = Convert.ToBoolean(result[x].Properties["mail"].Count > 0) ? result[x].Properties["mail"][0].ToString() : "";
wWWHomePage = Convert.ToBoolean(result[x].Properties["wWWHomePage"].Count > 0) ? result[x].Properties["wWWHomePage"][0].ToString() : "";
mobile = Convert.ToBoolean(result[x].Properties["mobile"].Count > 0) ? result[x].Properties["mobile"][0].ToString() : "";
homePhone = Convert.ToBoolean(result[x].Properties["homePhone"].Count > 0) ? result[x].Properties["homePhone"][0].ToString() : "";
employeeID = Convert.ToBoolean(result[x].Properties["employeeID"].Count > 0) ? result[x].Properties["employeeID"][0].ToString() : "";
title = Convert.ToBoolean(result[x].Properties["title"].Count > 0) ? result[x].Properties["title"][0].ToString() : "";
company = Convert.ToBoolean(result[x].Properties["company"].Count > 0) ? result[x].Properties["company"][0].ToString() : "";
department = Convert.ToBoolean(result[x].Properties["department"].Count > 0) ? result[x].Properties["department"][0].ToString() : "";
phone = Convert.ToBoolean(result[x].Properties["telephoneNumber"].Count > 0) ? result[x].Properties["telephoneNumber"][0].ToString() : "";
ou = result[x].Properties["distinguishedName"][0].ToString();
if (givenName.Length > 0)
{
Console.WriteLine(sAMAccountName);
Console.WriteLine(displayName);
Console.WriteLine(title);
Console.WriteLine(company);
Console.WriteLine(department);
Console.WriteLine(phone);
Console.WriteLine(streetAddress);
Console.WriteLine(ou);
Console.WriteLine();
}
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
Console.ReadLine();
}
}
}