2010-07-19 72 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 }