Guest User

Untitled

a guest
Jan 10th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.DirectoryServices.AccountManagement;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Threading.Tasks;
  7. using IdentityServer3.Core;
  8. using IdentityServer3.Core.Models;
  9. using IdentityServer3.Core.Services.Default;
  10. using System.DirectoryServices;
  11.  
  12. namespace SampleApp
  13. {
  14. public class ActiveDirectoryUserService : UserServiceBase
  15. {
  16. private const string DOMAIN = "Hoolio";
  17.  
  18. public override Task AuthenticateExternalAsync(ExternalAuthenticationContext context)
  19. {
  20. return Task.FromResult<AuthenticateResult>(null);
  21. }
  22.  
  23. public override Task AuthenticateLocalAsync(LocalAuthenticationContext context)
  24. {
  25. string username = context.UserName;
  26. string password = context.Password;
  27.  
  28. try
  29. {
  30. using (var pc = new PrincipalContext(ContextType.Domain, DOMAIN))
  31. {
  32. if (pc.ValidateCredentials(username, password))
  33. {
  34. using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username))
  35. {
  36. if (user != null)
  37. {
  38. context.AuthenticateResult = new AuthenticateResult(subject: user.Guid.ToString(), name: username);
  39. }
  40. }
  41. }
  42. }
  43. }
  44. catch
  45. {
  46.  
  47. }
  48.  
  49. return Task.FromResult(0);
  50. }
  51.  
  52. public override Task GetProfileDataAsync(ProfileDataRequestContext context)
  53. {
  54. List<Claim> _claims = new List<Claim>();
  55.  
  56. Claim _subject = context.Subject.Claims.FirstOrDefault();
  57.  
  58. if(_subject != null)
  59. {
  60. string username = GetUserIDFromSubject(_subject);
  61.  
  62. _claims = this.BuildProfileForUserName(username);
  63.  
  64. //Filter out the claims that weren't requested
  65. if (context.RequestedClaimTypes != null)
  66. {
  67. _claims = _claims.Where(c => context.RequestedClaimTypes.Contains(c.Type)).ToList();
  68. }
  69. }
  70.  
  71. context.IssuedClaims = _claims.AsEnumerable();
  72.  
  73. return Task.FromResult(0);
  74. }
  75.  
  76. private string GetUserIDFromSubject(Claim subject)
  77. {
  78. if (subject != null)
  79. {
  80. string _ldapPath = String.Format("LDAP://<GUID={0}>", subject.Value);
  81. var user = new DirectoryEntry(_ldapPath);
  82.  
  83. if (user != null)
  84. {
  85. return user.Properties["SamAccountName"].Value.ToString();
  86. }
  87. }
  88.  
  89. return null;
  90. }
  91. private List<Claim> BuildProfileForUserName(string username)
  92. {
  93. List<Claim> _claims = new List<Claim>();
  94.  
  95. if(!String.IsNullOrEmpty(username))
  96. {
  97. using (var pc = new PrincipalContext(ContextType.Domain, DOMAIN))
  98. {
  99.  
  100. using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username))
  101. {
  102. if (user != null)
  103. {
  104.  
  105. _claims.Add(new Claim(Constants.ClaimTypes.Subject, user.Guid.ToString()));
  106. _claims.Add(new Claim(Constants.ClaimTypes.GivenName, user.GivenName));
  107. _claims.Add(new Claim(Constants.ClaimTypes.FamilyName, user.Surname));
  108. _claims.Add(new Claim(Constants.ClaimTypes.Email, user.EmailAddress);
  109. _claims.Add(new Claim(Constants.ClaimTypes.IdentityProvider, "ActiveDirectory"));
  110.  
  111. foreach (string role in GetRolesForUser(username))
  112. {
  113. _claims.Add(new Claim(Constants.ClaimTypes.Role, role));
  114. }
  115. }
  116. }
  117. }
  118. }
  119.  
  120. return _claims;
  121. }
  122.  
  123. public override Task IsActiveAsync(IsActiveContext context)
  124. {
  125. context.IsActive = true;
  126. return Task.FromResult(0);
  127. }
  128.  
  129. public override Task PostAuthenticateAsync(PostAuthenticationContext context)
  130. {
  131. return Task.FromResult(0);
  132. }
  133.  
  134. public override Task PreAuthenticateAsync(PreAuthenticationContext context)
  135. {
  136. return Task.FromResult(0);
  137. }
  138.  
  139. public override Task SignOutAsync(SignOutContext context)
  140. {
  141. return Task.FromResult(0);
  142. }
  143.  
  144. private List<string> GetRolesForUser(string username)
  145. {
  146. List<string> result = new List<string>();
  147.  
  148. // establish domain context
  149. PrincipalContext _curDomain = new PrincipalContext(ContextType.Domain, DOMAIN);
  150.  
  151. // find your user
  152. UserPrincipal user = UserPrincipal.FindByIdentity(_curDomain, username);
  153.  
  154. // if found - grab its groups
  155. if (user != null)
  156. {
  157. PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
  158.  
  159. // iterate over all groups
  160. foreach (Principal p in groups)
  161. {
  162. // make sure to add only group principals
  163. if (p is GroupPrincipal)
  164. {
  165. result.Add(((GroupPrincipal)p).Name);
  166. }
  167. }
  168. }
  169.  
  170. return result;
  171. }
  172. }
  173. }
Add Comment
Please, Sign In to add comment