确定用户密码何时到期Active Directory,抛出错误的异常

时间:2015-08-28 11:27:31

标签: c# authentication exception-handling active-directory dotnetnuke

当用户密码设置为"下次登录时更改"在AD中,用户无法登录,因为他们的密码显示为不正确。我希望能够在身份验证时确定这一点并提示用户更改其密码。

我理解这样做我需要按照question中的描述捕获DirectoryServicesCOMException但是不会抛出该异常而是抛出的异常是Interop.COMException更高的继承hierarchy

这不包含确定密码是否需要更改或仅是错误的必需信息。

private bool IsAuthenticated(string Path, string UserName, string Password)
{
    try
    {
        DirectoryEntry userEntry = new DirectoryEntry(Path, UserName, Password); //, AuthenticationTypes.Signing)

        var obj = userEntry.NativeObject;
        return true;
    }
    catch (DirectoryServicesCOMException ex)
    {
        if (ex.ExtendedErrorMessage.Contains(" 773,") || ex.ExtendedErrorMessage.Contains("532"))
        {
            EventLogController.Instance.AddLog("Password expired", ex.ExtendedErrorMessage, Globals.GetPortalSettings(), -1, DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.ADMIN_ALERT);
            System.Web.HttpContext.Current.Response.Cookies["sync_expiryToken"].Value = "1";
            return true;
        }
        return false;
    }
    catch (COMException ex)
    {
        EventLogController.Instance.AddLog("Password wrong", ex.ToString(), Globals.GetPortalSettings(), -1, DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.ADMIN_ALERT);
        return false;
    }
    catch (Exception ex)
    {
        Exceptions.LogException(ex);
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我最终找到了答案,

某些身份验证类型只会抛出COMException而不是DirectoryServicesCOMException

在ldap上将身份验证类型设置为AuthenticationTypes.None解决了这个问题。