PrincipalContext.ValidateCredentials doesn't set lastLogon date for user

别等时光非礼了梦想. 提交于 2019-12-12 01:11:57

问题


I'm validating users in an Active Directory store as follows:

// using System.DirectoryServices.AccountManagement;
// located in System.DirectoryServices.AccountManagement.dll

using (var context = new PrincipalContext(ContextType.Domain, server, container, 
    ContextOptions.Negotiate, validateUsername, validatePassword))
{
    var valid = context.ValidateCredentials(validateUsername, validatePassword);
    if (valid)
    {
        Console.WriteLine("SUCCESS!");
        using (var userContext = UserPrincipal.FindByIdentity(context,
               IdentityType.SamAccountName, validateUsername))
        {
            Console.WriteLine("LastLogon = " + userContext.LastLogon);
        }
    }
    else
        Console.WriteLine("FAILED!");
}

The validation is successful, but the lastLogon value is never changed. It's essential that this value is changed when we authenticate a user in code due to other software using this value. I know ActiveDirectoryMembershipProvider authentication changes this property, so I'm wondering if there's a way I can use PrincipalContext (to reuse AD connections) but perform this validation to change the lastLogon value.


回答1:


Use lastLogonTimestamp. This is the field that gets updated in AD when you're attempting to connect via a PrincipalContext object.



来源:https://stackoverflow.com/questions/16606368/principalcontext-validatecredentials-doesnt-set-lastlogon-date-for-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!