2010-11-23 15 views
5

सी # कोड द्वारा उपयोगकर्ता खाते में पासवर्ड कैसे बदलें?सी # कोड द्वारा उपयोगकर्ता खाते में पासवर्ड कैसे बदलें?

+0

जो उपयोगकर्ता के System.DirectoryServices.AccountManagement संदर्भित करने के लिए की आवश्यकता होगी लेखा? डोमेन? कोई ऐप? सिस्टम? –

+0

कौन सा उपयोगकर्ता खाता ?????? – TalentTuner

+0

इस प्रश्न का एक डुप्ली हो सकता है (आप जिस पासवर्ड को बदलना चाहते हैं उसके आधार पर): http://stackoverflow.com/questions/234845/change-local-administrator-password-in-c –

उत्तर

5

सक्रिय निर्देशिका का उपयोग करना:

// Connect to Active Directory and get the DirectoryEntry object. 
// Note, ADPath is an Active Directory path pointing to a user. You would have created this 
// path by calling a GetUser() function, which searches AD for the specified user 
// and returns its DirectoryEntry object or path. See http://www.primaryobjects.com/CMS/Article61.aspx 
DirectoryEntry oDE; 
oDE = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure); 

try 
{ 
    // Change the password. 
    oDE.Invoke("ChangePassword", new object[]{strOldPassword, strNewPassword}); 
} 
catch (Exception excep) 
{ 
    Debug.WriteLine("Error changing password. Reason: " + excep.Message); 
} 

यहाँ आप उदाहरण है स्थानीय उपयोगकर्ता खाते में इसे बदलने के लिए:

http://msdn.microsoft.com/en-us/library/ms817839

अन्य विकल्प अंतर का उपयोग कर किया जा सकता है और फोन अप्रबंधित कोड: netapi32.dll

http://msdn.microsoft.com/en-us/library/aa370650(VS.85).aspx

+0

आपकी मदद के लिए धन्यवाद, मैं कैसे कर सकता हूं एडीपाथ प्राप्त करें? ActiveDS के साथ उपयोग करने की आवश्यकता है, यदि हां, तो कैसे? –

+0

क्षमा करें, कोड पर लिंक छिपी हुई है, हो सकता है कि स्टैक ओवरफ़्लो इस में सुधार कर सके :-) यहां एडी के सामान्य उपयोग और एडीपाथ बनाने के लिए लिंक है: http://www.primaryobjects.com/CMS/Article61 .aspx –

1
 DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
     DirectoryEntry grp; 
     grp = AD.Children.Find("test", "user"); 
     if (grp != null) 
     { 
      grp.Invoke("SetPassword", new object[] { "test" }); 
     } 
     grp.CommitChanges(); 
     MessageBox.Show("Account Change password Successfully"); 

"सभी उपयोगकर्ता

4

यहाँ बदलने के लिए व्यवस्थापक में चलाने के ऐसा करने का एक सरल तरीका है, लेकिन आप नेट 4.0 से

namespace PasswordChanger 
{ 
    using System; 
    using System.DirectoryServices.AccountManagement; 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      ChangePassword("domain", "user", "oldpassword", "newpassword"); 
     } 

     public static void ChangePassword(string domain, string userName, string oldPassword, string newPassword) 
     { 
      try 
      { 
       using (var context = new PrincipalContext(ContextType.Domain, domain)) 
       using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName)) 
       { 
        user.ChangePassword(oldPassword, newPassword); 
       } 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 
     } 
    } 
} 
+0

व्यवस्थापक विशेषाधिकारों के बिना भी महान काम करता है, धन्यवाद पॉल। – mberna

संबंधित मुद्दे