Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. private static TimeSpan? GetTimeRemainingUntilPasswordExpiration()
  2. {
  3.  
  4. Domain d = Domain.GetCurrentDomain();
  5. string domainName;
  6. domainName = System.Environment.UserDomainName;
  7.  
  8. //These next 2 lines is where I'm getting the error message
  9. PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
  10. UserPrincipal user = UserPrincipal.FindByIdentity(ctx, WindowsIdentity.GetCurrent().Name);
  11. using (System.Web.Hosting.HostingEnvironment.Impersonate())
  12. {
  13.  
  14.  
  15. TimeSpan? timeSpan = null;
  16.  
  17. if (user != null)
  18. {
  19. bool passwordNeverExpires = user.PasswordNeverExpires;
  20.  
  21. if (!passwordNeverExpires)
  22. {
  23. DirectoryEntry userEntry = (System.DirectoryServices.DirectoryEntry)user.GetUnderlyingObject();
  24. IADsUser native = (IADsUser)userEntry.NativeObject;
  25. timeSpan = native.PasswordExpirationDate - DateTime.Now;
  26. Console.WriteLine(user.GivenName + "'s password will expire on " + native.PasswordExpirationDate);
  27.  
  28. }
  29.  
  30.  
  31. }
  32. return timeSpan;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement