Esempio n. 1
0
        public static void UpdateLocalUser(string username, WindowsUser user, params string[] groups)
        {
            Debug.Assert(user.IsLocal && !string.IsNullOrEmpty(user.Name));
            int ret = 0;

            // rename
            if (!string.Equals(username, user.Name, StringComparison.OrdinalIgnoreCase))
            {
                ret = netapi.NetUserSetInfo(null, username, 0, ref user.Name, 0);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }
            }

            // attributes
            USER_INFO_4 u = new USER_INFO_4();

            {
                IntPtr pu = new IntPtr();
                ret = netapi.NetUserGetInfo(null, user.Name, 4, out pu);
                if (ret != 0)
                {
                    throw new Win32Exception(ret);
                }
                Marshal.PtrToStructure(pu, u);
                netapi.NetApiBufferFree(pu);
            }
            if (!string.IsNullOrEmpty(user.Password))
            {
                u.password         = user.Password;
                u.password_expired = user.PwdExpired;
            }
            u.full_name    = user.FullName;
            u.comment      = user.Comment;
            u.acct_expires = user.ExpireTime.HasValue ? (int)user.ExpireTime.Value.Subtract(util.TIME_MIN).TotalSeconds : util.TIMEQ_FOREVER;
            ret            = netapi.NetUserSetInfo(null, user.Name, 4, u, 0);
            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }

            // groups
            if (groups != null)
            {
                Array.ForEach <string>(groups, g => netapi.NetLocalGroupAddMembers(null, g, 3, ref user.Name, 1));
            }
        }
Esempio n. 2
0
        public static void AddLocalUser(WindowsUser user, params string[] groups)
        {
            Debug.Assert(user.IsLocal && !string.IsNullOrEmpty(user.Name));
            Debug.Assert(!string.IsNullOrEmpty(user.Password));

            int         flag = user.PwdExpired ? 0 : util.UF_DONT_EXPIRE_PASSWD;
            USER_INFO_1 u    = new USER_INFO_1()
            {
                name     = user.Name,
                password = user.Password,
                priv     = 1,
                home_dir = null,
                comment  = user.Comment,
                flags    = flag,
            };
            int ret = netapi.NetUserAdd(null, 1, u, 0);

            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }

            UpdateLocalUser(user.Name, user, groups);
        }