You're here: Snippet Directory » Microsoft .NET » Security (14)
Language:

Benutzer-Passwort im Active-Directory ändern

Language: Deutsch
Programming Language: C#
Published by: Olaf [not registered]
Last Update: 5/4/2006
Views: 2688

Description

Diese Klasse enthält Beispielcode, mit dem man das Benutzerpassword im Active Directory mittels .NET ändern kann.

Code

1 using System; 2 using System.DirectoryServices; 3 4 namespace FTProduction.Developer.UIWeb 5 { 6 /// <summary> 7 /// Summary description for UserManager. 8 /// </summary> 9 public class UserManager 10 { 11 private static UserInformation userInformation = null; 12 13 private UserManager() 14 { 15 16 } 17 18 /// <summary> 19 /// Instanz für das Objekt UserInformation holen bzw. erstellen 20 /// </summary> 21 public static UserInformation CurrentUserInformation 22 { 23 get 24 { 25 //Erstellt eine Instanz von UserInformation-Objekt wenn noch nicht vorhanden. 26 if (userInformation == null) 27 { 28 return new UserInformation(); 29 } 30 else 31 { 32 return userInformation; 33 } 34 } 35 } 36 37 /// <summary> 38 /// Ändert das Passwort von aktuallen Benutzer im Active Directory 39 /// </summary> 40 /// <param name="oldPassword">altes Passwort</param> 41 /// <param name="newPassword">neues Passwort</param> 42 /// <returns></returns> 43 internal static string ChangePassword(string oldPassword, string newPassword) 44 { 45 try 46 { 47 DirectorySearcher directorySearcher = new DirectorySearcher(); 48 //Suchkriterien für den aktiven Benutzer 49 directorySearcher.Filter = "(&(samaccountname=" + CurrentUserInformation.LogonUser + ")(objectClass=user))"; 50 //Suchen im Active Directory mittels gesetztem Suchfilter 51 SearchResultCollection src = directorySearcher.FindAll(); 52 //Funktionsaufruf für das Ändern vom PAsswort 53 object retVal = src[0].GetDirectoryEntry().Invoke("ChangePassword", new object[]{oldPassword,newPassword}); 54 } 55 catch(System.Reflection.TargetInvocationException ex) 56 { 57 //Fehler vom Active Directory zurückgeben, wenn beim Aufruf von "ChangePassword" 58 //ein Fehler auftritt 59 if (ex.InnerException != null) 60 { 61 return ex.InnerException.Message; 62 } 63 else 64 { 65 return ex.Message; 66 } 67 } 68 catch(Exception ex) 69 { 70 //Sonstige Fehlermeldung zurückgeben 71 return ex.Message; 72 } 73 74 //Wenn kein Fehler vorhanden, dann Bestätigung zurückgeben 75 return "Password changed successfull"; 76 } 77 78 public class UserInformation 79 { 80 private string logonUser = ""; 81 private string logonUsername = ""; 82 private string mail = ""; 83 private string mobile = ""; 84 85 internal UserInformation() 86 { 87 logonUser = getLogonUser(); 88 loadUserInformation(); 89 } 90 91 /// <summary> 92 /// Benutzername vom eingeloggten Benutzer ermitteln 93 /// </summary> 94 /// <returns></returns> 95 private static string getLogonUser() 96 { 97 //Ermitteln vom eingeloggten Benutzer 98 //Erforderlicher Web.config-Eintrag <identity impersonate="true" /> 99 string logonUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 100 //Wegschneiden vom Domainnamen 101 logonUser = logonUser.Substring(logonUser.LastIndexOf(@"\") + 1); 102 103 return logonUser; 104 } 105 106 /// <summary> 107 /// Laden der Benutzerinformationen 108 /// </summary> 109 private void loadUserInformation() 110 { 111 DirectorySearcher directorySearcher = new DirectorySearcher(); 112 //Suchkriterien für den aktiven Benutzer 113 directorySearcher.Filter = "(&(samaccountname=" + this.logonUser + ")(objectClass=user))"; 114 //Suchen im Active Directory mittels gesetztem Suchfilter 115 SearchResultCollection src = directorySearcher.FindAll(); 116 117 //Wenn der Benutzer gefunden wurde... 118 if (src.Count > 0) 119 { 120 //Ermitteln der Eigenschaften vom Suchresultat aus dem Active Directory 121 logonUsername = src[0].GetDirectoryEntry().Properties["displayName"].Value.ToString(); 122 mail = src[0].GetDirectoryEntry().Properties["mail"].Value.ToString(); 123 mobile = src[0].GetDirectoryEntry().Properties["mobile"].Value.ToString(); 124 } 125 } 126 127 public string LogonUser 128 { 129 get{return logonUser;} 130 } 131 132 public string LogonUsername 133 { 134 get{return logonUsername;} 135 } 136 137 public string Mail 138 { 139 get{return mail;} 140 } 141 142 public string Mobile 143 { 144 get{return mobile;} 145 } 146 } 147 } 148 } 149

One comment

1

hi

Wednesday, November 12, 2008 8:08:59 AM from Hi

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS