Directory Services Change Password : Password « Security « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Security » PasswordScreenshots 
Directory Services Change Password
Directory Services Change Password
  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Runtime.InteropServices;

namespace Homelink.AD.Utility
{
    public class Functions
    {
        public static void ChangePassword(string userID, string oldPassword, string newPassword)
        {           
            DirectoryEntry userEntry = Functions.GetUserEntry(userID);
            if (userEntry != null)
            {
                try
                {
                    // Check old password
                    // string userName = userEntry.Properties["CN"][0].ToString();
                    // DirectoryEntry en = new DirectoryEntry(userEntry.Path, userName, oldPassword, AuthenticationTypes.Secure);
                    // string cn = en.Properties["CN"][0].ToString();

                    userEntry.UsePropertyCache = true;
                    userEntry.Invoke("ChangePassword"new object[] { oldPassword, newPassword });
                    // userEntry.Invoke("SetPassword", new object[] { oldPassword });
                    userEntry.CommitChanges();
                    userEntry.Close();
                }
                catch (Exception e)
                {
                    Exception baseException = e.GetBaseException();
                    if (baseException is COMException)
                    {
                        COMException comException = baseException as COMException;
                        switch (comException.ErrorCode)
                        {
                            case -2147024810:
                                throw new Exception("The current password does not match our records. Please try again.");
                            case -2147022651:
                                throw new Exception("indicating password does not meet security requirements of the domain.");
                            case -2147023570:
                                throw new Exception("invalidate user or password.");
                            case -2147016657:
                                throw new Exception("invalidate user or password. Please try again. ");
                            default:
                                throw e;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("UserID = '" + userID + "' not found");
            }
        }

        public static DirectoryEntry GetUserEntry(string userID)
        {
            DirectoryEntry entry = Environment.GetDirectoryEntry();
            string filter = "(&(objectClass=user)(Pager=" + userID + "))";
            DirectorySearcher search = new DirectorySearcher(entry, filter);
            search.SearchScope = SearchScope.Subtree;
            SearchResult result = search.FindOne();

            if (result != null)
            {
                DirectoryEntry userEntry = new DirectoryEntry(result.Path, Environment.AD_AdminAccountName, Environment.AD_AdminPassword, AuthenticationTypes.Secure);
                return userEntry;
            }
            else
            {
                return null;
            }
        }
    }
}

   
    
  
Related examples in the same category
1.Utility class for encrypting passwords and comparing two encrypted passwords.
2.Password Encrypter
3.Send Password Email
4.Get Random Password Using GUID
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.