Skip to content

Instantly share code, notes, and snippets.

@stiano
Created January 16, 2018 11:32
Show Gist options
  • Save stiano/3231b818206c7afacb976236f51a612f to your computer and use it in GitHub Desktop.
Save stiano/3231b818206c7afacb976236f51a612f to your computer and use it in GitHub Desktop.
ActiveDirectoryService-GetUserByUserName
public class ActiveDirectoryService
{
private readonly NetworkCredential credentials;
public ActiveDirectoryService(NetworkCredential credentials)
{
this.credentials = credentials;
}
public GetUserByResponse GetUserBy(string username)
{
const string domain = "LDAP://TODO";
using (var directoryEntry = new DirectoryEntry(domain, credentials.UserName, credentials.Password))
using (var directorySearcher = new DirectorySearcher(
searchRoot: directoryEntry,
filter: "OU=Users" /* Improve filter if possible */
)
{
Filter = $"sAMAccountName={username}",
PageSize = 1,
})
{
var result = directorySearcher.FindOne();
return new GetUserByResponse(result);
}
}
public class GetUserByResponse
{
private readonly SearchResult result;
public GetUserByResponse(SearchResult result)
{
this.result = result;
}
public bool Found => result != null;
public bool NotFound => !Found;
public string Username => GetSingle("sAMAccountName", result);
public string Company => GetSingle("company", result);
public string Department => GetSingle("department", result);
public string Title => GetSingle("title", result);
public ICollection<string> MemberOf => GetAll("memberOf", result);
public override string ToString()
{
if (result == null)
return string.Empty;
var sb = new StringBuilder();
foreach (string name in result.Properties.PropertyNames)
{
for (var index = 0; index < result.Properties[name].Count; index++)
sb.AppendLine($"{name}-{index}: {result.Properties[name][index].ToString()}");
}
return sb.ToString();
}
#region Helpers
private static string GetSingle(string key, SearchResult result)
{
return result?.Properties[key]?[0]?.ToString();
}
private static ICollection<string> GetAll(string key, SearchResult result)
{
var items = new List<string>();
if (result == null)
return items;
var collection = result.Properties[key];
foreach (var item in collection)
{
items.Add(item.ToString());
}
return items;
}
#endregion
}
}
public class ActiveDirectoryServiceTests
{
private readonly NetworkCredential credentials = new NetworkCredential("<Username>", "<Password>");
[Explicit]
[TestCase("ok1")] // OSLO
[TestCase("ok2")] // OSLO
public void GetUserBy_ShouldSucceed(string username)
{
var service = new ActiveDirectoryService(credentials);
var result = service.GetUserBy(username);
Trace.WriteLine(result);
result.Found.Should().BeTrue();
result.Username.Should().Be(username);
result.MemberOf.Should().NotBeEmpty();
}
[Explicit]
[TestCase("shouldnotpass")]
public void GetUserBy_ShouldFail(string username)
{
var service = new ActiveDirectoryService(credentials);
var result = service.GetUserBy(username);
result.NotFound.Should().BeTrue();
result.Username.Should().Be(null);
result.MemberOf.Should().BeEmpty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment