如何让ActiveDirectoryMembershipProvider接受一个空密码?

我们正在开发一个Web应用程序,它使用表单身份验证和ActiveDirectoryMembershipProvider来针对Active Directory对用户进行身份验证。我们很快发现提供商不允许指定空/空密码,即使这在Active Directory中完全合法(如果没有预防性密码策略)。 礼貌的反射器:
private void CheckPassword(string password, int maxSize, string paramName)
{
    if (password == null)
    {
        throw new ArgumentNullException(paramName);
    }
    if (password.Trim().Length < 1)
    {
        throw new ArgumentException(SR.GetString("Parameter_can_not_be_empty", new object[] { paramName }), paramName);
    }
    if ((maxSize > 0) && (password.Length > maxSize))
    {
        throw new ArgumentException(SR.GetString("Parameter_too_long", new object[] { paramName, maxSize.ToString(CultureInfo.InvariantCulture) }), paramName);
    }
}
如果没有编写我们自己的自定义Provider,有没有办法使用.NET的魔力来覆盖这个功能?     
已邀请:
我不相信您可以在不创建派生类的情况下更改此行为,并覆盖调用私有CheckPassword方法的每个方法。我不会推荐这个选项,但我会建议您检查一下您的设计并询问是否允许在您的应用程序中使用空白密码。虽然它们在AD中有效但在实践中允许这种情况是不寻常的,并且它确实影响Windows网络中的其他内容,例如,我认为网络文件共享的默认设置禁止任何具有空密码的用户连接到共享。     
您可以考虑使用模拟,但我不知道您是否会遇到同样的问题。如果要授权用户,则可以使用模拟尝试“模拟”计算机上的用户。我不知道它是否有帮助,但我在另一周做了类似的事情。如果有任何帮助,请将代码放在下面.. :)
using System;  
using System.Runtime.InteropServices;  

public partial class Test_Index : System.Web.UI.Page {  
protected void Page_Load(object sender, EventArgs e)
{        
    IntPtr ptr = IntPtr.Zero;
    if (LogonUser("USERNAME", "", "LEAVE-THIS-BLANK", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, ref ptr))
    {
        using (System.Security.Principal.WindowsImpersonationContext context = new System.Security.Principal.WindowsIdentity(ptr).Impersonate())
        {
            try
            {
                // Do do something
            }
            catch (UnauthorizedAccessException ex)
            {
                // failed to do something
            }

            // un-impersonate user out
            context.Undo();
        }
    }
    else
    {
        Response.Write("login fail");
    }
}

#region imports

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CloseHandle(IntPtr handle);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr existingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr duplicateTokenHandle);

#endregion

#region logon consts

// logon types 
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;

// logon providers 
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT50 = 3;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT35 = 1;
#endregion  }
    

要回复问题请先登录注册