원본 : http://msdn.microsoft.com/ko-kr/library/ms180915(VS.80).aspx
(즐겨찾기에 있는 정보로 링크 변경이나, 사이트 변경에 따라 정보가 유실될 것 같아 홈페이지에 등록합니다. 아래의 문건의 권리는 모두 Microsoft 에 귀속됩니다.)
이 항목에는 사용자 암호 관리에 대한 정보 및 코드 예가 들어 있습니다.
다음 C# 코드 예에서는 IADsUser::SetPasswordadsi.iadsuser_setpassword 메서드를 호출하여 사용자 암호를 설정하는 방법을 보여 줍니다. IADsUser::SetPassword에 대한 자세한 내용은 MSDN Library(http://msdn.microsoft.com/library)의 "IADsUser::SetPassword"를 참조하십시오.
usr.Invoke("SetPassword", SecurelyStoredPassword);
다음 C# 코드 예에서는 IADsUser::ChangePasswordadsi.iadsuser_changepassword 메서드를 호출하여 사용자 암호를 변경하는 방법을 보여 줍니다. IADsUser::ChangePassword에 대한 자세한 내용은 MSDN Library(http://msdn.microsoft.com/library)의 "IADsUser::ChangePassword"를 참조하십시오.
usr.Invoke("ChangePassword", OldSecurelyStoredPassword, NewSecurelyStoredPassword);
다음 C# 코드 예에서는 다음 로그온 시 사용자 암호를 변경할 수 있도록 설정하는 방법을 보여 줍니다. 이 경우 pwdLastSetadschema.a_pwdlastset 속성을 off(-1)로 설정합니다. adschema pwdLastSet 특성에 대한 자세한 내용은 MSDN Library(http://msdn.microsoft.com/library)의 "pwdLastSet" 또는 "Pwd-Last-Set attribute"를 참조하십시오.
usr.Properties["pwdLastSet"].Value = -1; // To turn on, set this value to 0. usr.CommitChanges();
다음 C# 코드 예에서는 암호를 변경하는 사용자 권한을 거부하도록 ACE를 설정하는 기능을 보여 줍니다. 이 경우 ntSecurityDescriptor 속성을 가져올 수 있도록 IADsSecurityDescriptor에 액세스하는 데 ADSI 액세스에 COM 상호 운용성 사용를 사용합니다. 그런 다음 IADsAccessControlList를 사용하여 보안 설명자의 DACL을 가져오고 IADsAccessControlEntry를 사용하여 AceType, AceFlags, Trustee, Flags, ObjectType 및 AccessMask 속성을 가져옵니다. AceType 플래그는 ADS_ACETYPE_ENUM에 정의되어 있습니다. AceFlags는 ADS_FLAGTYPE_ENUM에 정의되어 있습니다. AccessMask 플래그는 ADS_RIGHTS_ENUM에 정의되어 있습니다.
using System; using System.DirectoryServices; using ActiveDs; static void DenyChangePassword(DirectoryEntry User) { const string PASSWORD_GUID = "{ab721a53-1e2f-11d0-9819-00aa0040529b}"; const int ADS_UF_PASSWORD_EXPIRED=0x800000; const int ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION=0x1000000; string[] trustees = new string[]{@"NT AUTHORITY\SELF","EVERYONE"}; ActiveDs.IADsSecurityDescriptor sd = (ActiveDs.IADsSecurityDescriptor) User.Properties["ntSecurityDescriptor"].Value; ActiveDs.IADsAccessControlList acl = (ActiveDs.IADsAccessControlList) sd.DiscretionaryAcl; ActiveDs.IADsAccessControlEntry ace = new ActiveDs.AccessControlEntry(); foreach(string trustee in trustees) { ace.Trustee = trustee; ace.AceFlags = 0; ace.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_DENIED_OBJECT; ace.Flags = (int)ActiveDs.ADS_FLAGTYPE_ENUM.ADS_FLAG_OBJECT_TYPE_PRESENT; ace.ObjectType = PASSWORD_GUID; ace.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_DS_CONTROL_ACCESS; acl.AddAce(ace); } sd.DiscretionaryAcl = acl; User.Properties["ntSecurityDescriptor"].Value = sd; User.CommitChanges(); }
다음 C# 예에서는 암호를 변경하는 사용자 권한을 거부하도록 ACE를 설정하는 기능을 보여 줍니다. 이 예에서는 .NET Framework 2.0에서 사용할 수 있는 관리되는 ACL 기능을 사용합니다.
using System; using System.DirectoryServices; using System.Security.Principal; using System.Security.AccessControl; static void DenyChangePassword(DirectoryEntry user) { // Create a Guid that identifies the Change Password right. Guid changePasswordGuid = new Guid("{AB721A53-1E2F-11D0-9819-00AA0040529B}"); // Get the ActiveDirectorySecurity for the user. ActiveDirectorySecurity userSecurity = user.ObjectSecurity; // Create a SecurityIdentifier object for "everyone". SecurityIdentifier everyoneSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); // Create a SecurityIdentifier object for "self". SecurityIdentifier selfSid = new SecurityIdentifier(WellKnownSidType.SelfSid, null); // Create an access rule to allow everyone the change password // right. // This is used to remove any existing access rules. ActiveDirectoryAccessRule allowEveryone = new ActiveDirectoryAccessRule( everyoneSid, ActiveDirectoryRights.ExtendedRight, AccessControlType.Allow, changePasswordGuid); // Create an access rule to deny everyone the change password right. ActiveDirectoryAccessRule denyEveryone = new ActiveDirectoryAccessRule( everyoneSid, ActiveDirectoryRights.ExtendedRight, AccessControlType.Deny, changePasswordGuid); // Create an access rule to allow self the change password right. // This is used to remove any existing access rules. ActiveDirectoryAccessRule allowSelf = new ActiveDirectoryAccessRule( selfSid, ActiveDirectoryRights.ExtendedRight, AccessControlType.Allow, changePasswordGuid); // Create an access rule to deny self the change password right. ActiveDirectoryAccessRule denySelf = new ActiveDirectoryAccessRule( selfSid, ActiveDirectoryRights.ExtendedRight, AccessControlType.Deny, changePasswordGuid); // Remove any existing rule that gives "everyone" the change // password right. userSecurity.RemoveAccessRuleSpecific(allowEveryone); // Add a new access rule to deny "everyone" the change password // right. userSecurity.AddAccessRule(denyEveryone); // Remove any existing rule that gives "self" the change password // right. userSecurity.RemoveAccessRuleSpecific(allowSelf); // Add a new access rule to deny "self" the change password right. userSecurity.AddAccessRule(denySelf); // Commit the changes. user.CommitChanges(); }
다음 코드 예에서는 암호가 만료되지 않도록 설정하는 방법을 보여 줍니다. 이 경우 ADS_USER_FLAG_ENUM에 정의되어 있는 ADS_UF_DONT_EXPIRE_PASSWD 플래그를 설정할 수 있도록 userAccountControl 속성에 액세스하기 위해 Properties 메서드를 사용합니다.
using System; using System.DirectoryServices; using ActiveDs; static void DontExpirePassword(DirectoryEntry User) { int val; const int ADS_UF_DONT_EXPIRE_PASSWD =0x10000; val = (int) User.Properties["userAccountControl"].Value; User.Properties["userAccountControl"].Value = val | ADS_UF_DONT_EXPIRE_PASSWD; User.CommitChanges(); }