Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static TimeSpan? GetTimeRemainingUntilPasswordExpiration()
- {
- Domain d = Domain.GetCurrentDomain();
- string domainName;
- domainName = System.Environment.UserDomainName;
- //These next 2 lines is where I'm getting the error message
- PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
- UserPrincipal user = UserPrincipal.FindByIdentity(ctx, WindowsIdentity.GetCurrent().Name);
- using (System.Web.Hosting.HostingEnvironment.Impersonate())
- {
- TimeSpan? timeSpan = null;
- if (user != null)
- {
- bool passwordNeverExpires = user.PasswordNeverExpires;
- if (!passwordNeverExpires)
- {
- DirectoryEntry userEntry = (System.DirectoryServices.DirectoryEntry)user.GetUnderlyingObject();
- IADsUser native = (IADsUser)userEntry.NativeObject;
- timeSpan = native.PasswordExpirationDate - DateTime.Now;
- Console.WriteLine(user.GivenName + "'s password will expire on " + native.PasswordExpirationDate);
- }
- }
- return timeSpan;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement