在Active Directory中,如果用户的帐户被禁用然后启用,则默认情况下,用户必须在首次登录时更改其密码。我正在努力能够使用C#以编程方式检测到这一点?如果用户必须重置其属性,是否有一个属性设置或沿着这些行?
假设我有一个指向用户的DirecotryEntry
对象:
DirectoryEntry user = ...
是否有可以使用的属性:
user.Properties[someProperty];
答案 0 :(得分:8)
答案 1 :(得分:6)
以下是我写的内容。不完全回答你的问题,但对后来阅读它的人有用。
重要的部分来自PrincipalContext on。 上面的所有内容就是我尝试总是以正确的大写字母返回AdName的方式。
注意这只是代码做的第一个答案,使用用户主体而不是DE来测试LastPasswordSet。
埃里克 -
private bool TestAdShouldChangePassword( string adUser )
{
try
{
string adName = "";
MembershipUser mu = Membership.GetUser( adUser );
if ( mu != null )
{
IStudentPortalLoginBLL splBll = ObjectFactory.GetInstance< IStudentPortalLoginBLL >();
adName = splBll.GetCleanAdName( adUser );// I wrote this is just pulls outhe name and fixes the caplitalization - EWB
PrincipalContext pctx = new PrincipalContext( System.DirectoryServices.AccountManagement.ContextType.Domain );
UserPrincipal p = UserPrincipal.FindByIdentity( pctx, adName );
if ( p == null )
return false;
if ( p.LastPasswordSet.HasValue == false && p.PasswordNeverExpires == false )
{
return true;
}
}
}
catch ( MultipleMatchesException mmex )
{
log.Error ( "TestAdShouldChangePassword( ad user = '" + adUser + "' ) - Exception finding user, can't determine if ad says to change password, returing false : Ex = " + mmex.ToString() );
}
return false;
}
答案 2 :(得分:3)
能够使用以下代码获取它:
public bool PasswordRequiresChanged(string userName)
{
DirectoryEntry user = GetUser(userName); //A directory entry pointing to the user
Int64 pls;
int uac;
if (user != null && user.Properties["pwdLastSet"] != null && user.Properties["pwdLastSet"].Value != null)
{
pls = ConvertADSLargeIntegerToInt64(user.Properties["pwdLastSet"].Value);
}
else
{
throw new Exception("Could not determine if password needs reset");
}
if (user != null && user.Properties["UserAccountControl"] != null && user.Properties["UserAccountControl"].Value != null)
{
uac = (int)user.Properties["UserAccountControl"].Value;
}
else
{
throw new Exception("Could not determine if password needs reset");
}
return (pls == 0) && ((uac & 0x00010000) == 0) ? true : false;
}
private static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
{
var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
}
答案 3 :(得分:1)
var username = "radmin";
var adContext = new PrincipalContext(ContextType.Domain, adLocation, adContainer, adAdminUsername, adAdminPassword);
var user = UserPrincipal.FindByIdentity(adContext, username);
Console.WriteLine(user.LastPasswordSet);
如果LastPasswordSet具有空值,则“用户必须在下次登录时更改密码”。