Opc.Ua.Configuration.AccountInfo.CreateUser C# (CSharp) Method

CreateUser() public static method

Creates a new NT user account.
public static CreateUser ( string username, string password ) : AccountInfo
username string
password string
return AccountInfo
        public static AccountInfo CreateUser(string username, string password)
        {
            int dwLevel = 1;
            int dwError = 0;
            int nStatus;

            //
            // Set up the USER_INFO_1 structure.
            //  USER_PRIV_USER: name identifies a user, 
            //    rather than an administrator or a guest.
            //  UF_SCRIPT: required for LAN Manager 2.0 and
            //    Windows NT and later.
            //
            USER_INFO_1 ui = new USER_INFO_1();

            ui.usri1_name = username;
            ui.usri1_password = password;
            ui.usri1_priv = USER_PRIV_USER;
            ui.usri1_home_dir = null;
            ui.usri1_comment = username;
            ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD | UF_PASSWD_CANT_CHANGE;
            ui.usri1_script_path = null;

            IntPtr pInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(USER_INFO_1)));
            Marshal.StructureToPtr(ui, pInfo, false);
            
            // try to add the user.
            try
            {
               nStatus = NetUserAdd(
                   null, 
                   dwLevel,
                   pInfo,
                   out dwError);
            }
            finally
            {
                Marshal.DestroyStructure(pInfo, typeof(USER_INFO_1));
                Marshal.FreeCoTaskMem(pInfo);
            }

            if (nStatus != NERR_Success)  // maybe account exists, so just set the password
            {
                // set the password.
                dwLevel = 1003;
                USER_INFO_1003 ui1003;
                ui1003.usri1003_password = password;

                pInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(USER_INFO_1003)));
                Marshal.StructureToPtr(ui1003, pInfo, false);

                try
                {
                    nStatus = NetUserSetInfo(
                        null,
                        username,
                        dwLevel,
                        pInfo,
                        out dwError);
                }
                finally
                {
                    Marshal.DestroyStructure(pInfo, typeof(USER_INFO_1003));
                    Marshal.FreeCoTaskMem(pInfo);
                }

	            // set the account flags (e.g. enable the account if disabled)
	            dwLevel = 1008;
	            USER_INFO_1008 ui1008;
	            ui1008.usri1008_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD | UF_PASSWD_CANT_CHANGE;
        	
                pInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(USER_INFO_1003)));
                Marshal.StructureToPtr(ui1003, pInfo, false);

                try
                {
                    nStatus = NetUserSetInfo(
                        null,
                        username,
                        dwLevel,
                        pInfo,
                        out dwError);
                }
                finally
                {
                    Marshal.DestroyStructure(pInfo, typeof(USER_INFO_1008));
                    Marshal.FreeCoTaskMem(pInfo);
                }
            }

            if (nStatus != NERR_Success)
            {
                return null;
            }

            return Create(username);
        }