2010-07-19 52 views
0

我们正在开发一个Web应用程序,该应用程序使用表单身份验证和ActiveDirectoryMembershipProvider根据Active Directory对用户进行身份验证。我们很快发现提供程序不允许指定空白/空密码,即使这在Active Directory中是完全合法的(只要预防性密码策略不适用)。如何使ActiveDirectoryMembershipProvider接受空密码?

礼貌反射:

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); 
    } 
} 

短编写我们自己的自定义提供的,有没有什么办法来覆盖使用.NET的魔法此功能?

回答

1

我不敢相信你可以改变这种行为,而无需创建一个派生类和overiding每次调用私有CheckPassword方法的方法。我不会推荐这个选项,但是我会建议你检查一下你的设计,并询问它是否适用于允许你的应用程序使用空密码。虽然它们在AD中有效,但在实践中允许这种做法并不常见,并且它影响了Windows网络中的其他事物,例如,我认为网络文件共享的默认设置不允许任何用户使用空密码连接到共享。

+0

您可以连接到文件共享和映射网络驱动器等与一个空/空密码,除了ActiveDirectoryMembershipProvider之外,它全面支持。你是对的,没有办法在没有创建子类的情况下重写这种行为。 – fletcher 2010-07-28 14:02:20

0

你也许可以看看使用模拟,但我不知道你是否会有同样的问题。如果是授权用户,那么您可以使用模拟来尝试并“模拟”机器上的用户。我不知道这是否有帮助,但我在这个星期做了类似的事情。已经把下面的代码,如果有任何这有助于.. :)

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 }