Overview:
Now a day’s many companies are adopting registration and authentication for their employees for In-House applications at active directory domain level. In-House applications can register only those users which are already working inside companies’ domain.
Here I am writing this blog which will let developer knows that how in house applications can manage below two processes:

Pop up with Search results:

using System;
using System.DirectoryServices.AccountManagement;
using System.Security.Claims;
using Microsoft.Owin.Security;
using System.DirectoryServices;
using System.Collections.Generic;
namespace ADdemoProject
{
/// <summary>
/// Class AdAuthenticationService
/// </summary>
public class AdAuthenticationService
{
public List<UserMaster> GetUserFromAD(string name)
{
var domainContext = new PrincipalContext(ContextType.Domain);
var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, “Domain Users”);
UserPrincipal user = new UserPrincipal(domainContext);
user.Enabled = true;
user.SamAccountName = name;
PrincipalSearcher pSearch = new PrincipalSearcher();
pSearch.QueryFilter = user;
PrincipalSearchResult<Principal> results = pSearch.FindAll();
List<UserMaster> lstUsers = new List<UserMaster>();
foreach (var item in results)
{
UserMaster objUser = new UserMaster();
objUser.UserName = item.SamAccountName;
objUser.FullName = item.DisplayName;
lstUsers.Add(objUser);
}
return lstUsers;
}
}
}
What is ASP.NET Web API and How it works?


using System;
using System.DirectoryServices.AccountManagement;
using System.Security.Claims;
using Microsoft.Owin.Security;
using System.DirectoryServices;
using System.Collections.Generic;
namespace ADdemoProject
{
/// <summary>
/// Class AdAuthenticationService
/// </summary>
public class AdAuthenticationService
{
public AuthenticationResult SignIn(String username, String password)
{
// authenticates against your Domain AD
ContextType authenticationType = ContextType.Domain;
PrincipalContext principalContext = new PrincipalContext(authenticationType);
bool isAuthenticated = false;
UserPrincipal userPrincipal = null;
try
{
isAuthenticated = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate);
if (isAuthenticated)
{
userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);
}
}
catch (Exception)
{
isAuthenticated = false;
userPrincipal = null;
}
if (!isAuthenticated || userPrincipal == null)
{
return new AuthenticationResult(“Username or Password is not correct”);
}
if (userPrincipal.IsAccountLockedOut())
{
// here can be a security related discussion weather it is worth
// revealing this information
return new AuthenticationResult(“Your account is locked.”);
}
if (userPrincipal.Enabled.HasValue && userPrincipal.Enabled.Value == false)
{
// here can be a security related discussion weather it is worth
// revealing this information
return new AuthenticationResult(“Your account is disabled”);
}
var identity = CreateIdentity(userPrincipal);
authenticationManager.SignOut(MyAuthentication.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);
return new AuthenticationResult();
}
}
}
Author – Sahil Joshi