/// <summary>
        /// http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q187/5/35.asp&NoWebContent=1 How To Change Passwords Programmatically in Windows NT
        /// http://support.microsoft.com/kb/149427 How to Change User Password at Command Prompt
        ///     net user user_name * /domain
        ///         When you are prompted to type a password for the user, type the new password, not the existing password. After you type the new password, the system prompts you to retype the password to confirm. The password is now changed.
        ///     net user user_name new_password
        /// </summary>
        public static int NetUserSetInfoPassword(string servername, string username, string newpassword)
        {
            int       nStatus;
            USER_INFO level = USER_INFO.USER_INFO_1003;

            System.UInt32  parm_err;
            System.IntPtr  userInfoPtr = IntPtr.Zero;
            USER_INFO_1003 userInfo    = new USER_INFO_1003();

            userInfo.usri1003_password = newpassword;

            userInfoPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(userInfo));
            Marshal.StructureToPtr(userInfo, userInfoPtr, false);

            nStatus = NetUserSetInfo(servername, username, (System.UInt32)level, userInfoPtr, out parm_err);
            NetApiBufferFree(userInfoPtr);
            if (nStatus == NERR_Success)
            {
                System.Console.WriteLine("Password has been changed successfully");
            }
            else
            {
                System.Console.WriteLine("A system error has occurred: {0} {1}", nStatus, MethodBase.GetCurrentMethod());
            }
            return(nStatus);
        }
示例#2
0
        NetChangePassword(
            string servername,
            string username,
            string password
            )
        {
            uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS;

            if (String.IsNullOrEmpty(servername))
            {
                servername = null;
            }
            if (String.IsNullOrEmpty(username))
            {
                username = null;
            }
            if (String.IsNullOrEmpty(password))
            {
                password = null;
            }

            USER_INFO_1003 ui1003 = new USER_INFO_1003();
            ui1003.usri1003_password = password;

            IntPtr bufptr = Marshal.AllocHGlobal(Marshal.SizeOf(ui1003));

            try
            {
                if (!NetApiInitCalled)
                {
                    result = NetApiInit();
                    if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS)
                    {
                        return result;
                    }

                    NetApiInitCalled = true;
                }

                Marshal.StructureToPtr(ui1003, bufptr, false);
                result = (uint)NetUserSetInfo(servername, username, 1003, bufptr, IntPtr.Zero);
            }
            catch (Exception)
            {
                result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE;
            }
            finally
            {
                Marshal.DestroyStructure(bufptr, ui1003.GetType());
                Marshal.FreeHGlobal(bufptr);
            }

            return result;
        }
 internal static extern UInt32  NetUserSetInfo(string servername, string username, UInt32 level, ref USER_INFO_1003 userinfo, out UInt32 parm_err);