2009-06-26 7 views
1

WindowsAccountがパスワードで保護されているかどうかを知りたい。C#:Windowsアカウントがパスワードで保護されている場合の把握方法

セキュリティ上の理由から、パスワードは取得できませんが、明らかですが、パスワードが設定されているかどうかを確認する方法が必要です。

public bool IsAccountPasswordProteced(String userName) 
{ 
    String entryString = "WinNT://" + Environment.MachineName + ",Computer"; 
    DirectoryEntry dirEntry = new DirectoryEntry(entryString); 

    DirectoryEntry user = dirEntry.Children.Find(userName, "User"); 

    // First try was to check the ADS_UF_PASSWD_NOTREQD flag. 
    // If this flag is set, the account has no password, 
    // but if not, both is possible. 
    int userFlags = (int)user.Properties["UserFlags"].Value; 
    return (userFlags & (int)ActiveDs.ADS_USER_FLAG.ADS_UF_PASSWD_NOTREQD) == 0; 

    // Second try was to check the PasswordAge. 
    int pwAge = (int)user.Properties["PasswordAge"].Value; 
    return pwAge > 0; 
} 
+2

彼らは私が「WindowsIdentityを持つ2つのアカウントの「identity.AuthenticationType」の値をテストしたP – DevinB

答えて

0

あなたは、与えられたアカウントのUPN名またはユーザートークン(userオブジェクトのプロパティのうちの1つを教えてくれなければならない)、そして、あなたはこのようWindowsIdentityクラスのものを使用することができるはずを得ることができる場合:

using System.Security.Principal; 

// ... 

var identity = new WindowsIdentity("foo-UPN"); 
var requiresPassword = identity.AuthenticationType != string.Empty; 
+0

を=としてログインしよう:

私はこの2つのリンクを使用していました。 GetCurrent() "、1つはパスワード付きで、もう1つは" NTLM "です。 "DirectoryEntry user"には "AuthenticationType"もありますが、テストしたアカウントでは常に「安全」です。 – Fry

3

もっと良い方法がない場合、私はLogonUser関数を使用しますが、それは私が探していた方法ではありません。しかし、それは何よりも優れています。 ローカルアカウント(LogonTypeのためネットワーク経由ではありません)と有効なアカウントからこれを使用すると、そのアカウントは機能するはずです。

Calling LogonUser

Detect empty Passwords

public bool IsAccountPasswordProtected(String userName) 
{ 
    String entryString = "WinNT://" + Environment.MachineName + ",Computer"; 
    DirectoryEntry dirEntry = new DirectoryEntry(entryString); 

    DirectoryEntry user = dirEntry.Children.Find(userName, "User"); 

    ////EDIT: this flag can also be set, if the account has a password 
    //int userFlags = (int)user.Properties["UserFlags"].Value; 
    //if ((userFlags & (int)ActiveDs.ADS_USER_FLAG.ADS_UF_PASSWD_NOTREQD) != 0) 
    // return false; 

    IntPtr token; 
    bool result = LogonUser(
     user.Name, Environment.UserDomainName, 
     "", 
     LogonTypes.Interactive, 
     LogonProviders.Default, 
     out token); 
    if (result) 
    { 
     CloseHandle(token); 
     return false; 
    } 
    else 
    { 
     int err = Marshal.GetLastWin32Error(); 
     if (err == 1327) // ERROR_ACCOUNT_RESTRICTION 
      return false; 

     //if(err == 1331) // ERROR_ACCOUNT_DISABLED 

     return true; 
    } 
} 

[DllImport("advapi32.dll", SetLastError = true)] 
static extern bool LogonUser(
    string principal, 
    string authority, 
    string password, 
    LogonTypes logonType, 
    LogonProviders logonProvider, 
    out IntPtr token); 
[DllImport("kernel32.dll", SetLastError = true)] 
static extern bool CloseHandle(IntPtr handle); 
enum LogonTypes : uint 
{ 
    Interactive = 2, 
    Network, 
    Batch, 
    Service, 
    NetworkCleartext = 8, 
    NewCredentials 
} 
enum LogonProviders : uint 
{ 
    Default = 0, // default for platform (use this!) 
    WinNT35,  // sends smoke signals to authority 
    WinNT40,  // uses NTLM 
    WinNT50  // negotiates Kerb or NTLM 
} 
関連する問題