Active Directory With ASP - Net MVC (.NET) - CodeProject
Active Directory With ASP - Net MVC (.NET) - CodeProject
NET) - CodeProject
Introduction
When it comes to access Microsoft's Active Directory using C#, a lot of people get confused how to get started. This article attempts
to show you how to communicate with active directory using C# in a simple way. I make a simple web application interact with
active directory using ASP.NET MVC .This application performs only three operations on active directory:
Background
You should have some basic knowledge with ASP.NET MVC.
Active Directory
1. Install Windows Server 2012 R2.
2. Install Active Directory Domain Service.
3. Create new domain in a new forest. I named it “MBS.Com”.
4. Add organizational unit and named it “DevOU.
5. Add some users and groups in OU.
https://www.codeproject.com/Articles/1279198/Active-Directory-with-ASP-NET-MVC-NET?display=Print 1/6
07/03/2019 Active Directory with ASP.NET MVC (.NET) - CodeProject
1. System.DirectoryServices.ActiveDirectory
2. System.DirectoryServices.AccountManagement (this is what I used)
Note
In my case, IIS server and directory domain controller reside on the same machine to run task successfully.
Add Action called HomePage in your Controller containing two buttons, one for Users and other for Groups.
@{
ViewBag.Title = "HomePage";
}
@*<h2>HomePage</h2>*@
<br />
<br />
<div class="row text-center">
<div class="col lg-6">
@Html.ActionLink
("Users", "GetAllUsers", "Home", null, new { @class = "btn btn-primary" })
@Html.ActionLink
("Groups", "GetAllGroups", "Home", null, new { @class = "btn btn-primary" })
</div>
</div>
https://www.codeproject.com/Articles/1279198/Active-Directory-with-ASP-NET-MVC-NET?display=Print 2/6
07/03/2019 Active Directory with ASP.NET MVC (.NET) - CodeProject
Then we will implement three methods which will be performed on Active Directory: Get All Users, Get All Groups, Set
Password:
//if you want to get Groups of Specific OU you have to add OU Name in Context
public static List<User> GetallAdUsers()
{
List<User> AdUsers = new List<User>();
//MBS.com My Domain Controller which i created
//OU=DevOU --Organizational Unit which i created
//and create users and groups inside it
var ctx = new PrincipalContext(ContextType.Domain, "MBS","OU=DevOU,DC=MBS,DC=com");
UserPrincipal userPrin = new UserPrincipal(ctx);
userPrin.Name = "*";
var searcher = new System.DirectoryServices.AccountManagement.PrincipalSearcher();
searcher.QueryFilter = userPrin;
var results = searcher.FindAll();
foreach (Principal p in results)
{
AdUsers.Add(new User { DisplayName = p.DisplayName,
Samaccountname = p.SamAccountName });
}
return AdUsers;
}
//if you want to get all Groups of Specific OU you have to add OU Name in Context
public static List<Group> GetallGroups()
{
List<Group> AdGroups = new List<Group>();
var ctx = new PrincipalContext(ContextType.Domain, "MBS", "OU=DevOU,DC=MBS,DC=com");
GroupPrincipal _groupPrincipal = new GroupPrincipal(ctx);
}
return AdGroups;
}
https://www.codeproject.com/Articles/1279198/Active-Directory-with-ASP-NET-MVC-NET?display=Print 3/6
07/03/2019 Active Directory with ASP.NET MVC (.NET) - CodeProject
@model IEnumerable<ActiveDirectory.Models.User>
@{
ViewBag.Title = "GetAllUsers";
}
<br />
<form>
<div class="form-group">
<label for="SearchInput" class="col-sm-2 col-form-label">Search for User</label>
<div class="col-md-10">
<input type="text" class="form-control" id="SearchInput"
<br />
<br />
@Html.Raw(TempData["msg"])
</table>
@section scripts
{
<script>
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("SearchInput");
filter = input.value.toUpperCase();
table = document.getElementById("tblUsers");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
}
https://www.codeproject.com/Articles/1279198/Active-Directory-with-ASP-NET-MVC-NET?display=Print 4/6
07/03/2019 Active Directory with ASP.NET MVC (.NET) - CodeProject
@model IEnumerable<ActiveDirectory.Models.Group>
@{
ViewBag.Title = "GetAllGroups";
}
<br />
</tr>
}
</table>
https://www.codeproject.com/Articles/1279198/Active-Directory-with-ASP-NET-MVC-NET?display=Print 5/6
07/03/2019 Active Directory with ASP.NET MVC (.NET) - CodeProject
Notes
All these functionalities work on specific organizational unit I have created “DevOU”.
To get All Users and groups of Active Directory, just remove “OU” from path in the context.
History
7th March, 2019: Initial version
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2019 by Mina bedier
Web05 | 2.8.190306.1 | Last Updated 7 Mar 2019 Everything else Copyright © CodeProject, 1999-2019
https://www.codeproject.com/Articles/1279198/Active-Directory-with-ASP-NET-MVC-NET?display=Print 6/6