2012-12-21 129 views
1

我正在使用以下代码重置用户的Active Directory密码。如何以编程方式设置Active Directory密码时需要更改密码?

using (var context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName)) 
    { 
     user.SetPassword("newpassword"); 
    } 
} 

但我需要能够要求用户在登录后的第一时间来更改密码。我找不到,没有工作的方法或设置或属性,但是。很明显,这可以做到,我只是不知道如何!

回答

1

你需要到期的新创建的密码马上 - 试试这个:

using (var context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName)) 
    { 
     user.SetPassword("newpassword"); 
     user.ExpirePasswordNow((); 
    } 
} 

更多细节

+0

MSDN docs on ExpirePasswordNow为什么你需要'SetPassword'如果你要过期了吗? – rae1

+0

@ rae1n:如果您有一个可以生成系统密码的系统,您希望用户能够使用该系统生成的密码登录一次,但要求他立即更改密码。对我来说完全有意义。 –

+0

正是医生的命令。在实际的代码中尝试了这一点,它运行得很顺畅。 – Cyberherbalist

相关问题