Loading
0

C#创建系统用户并设置8位数随机密码并将制定目录权限设置为该用户完全控制权限

技术小学生微信公众号
腾讯云服务器大促销。
华为服务器
本代码功能如下:
1、创建一个系统用户,并随机设置一个8位的数字+字母大小写密码
2、将指定目录权限设置为nginx及administrators 完全控制权限
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.IO;
using System.Security.AccessControl;
 
namespace testadduser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static char[] constant =
      {
        '0','1','2','3','4','5','6','7','8','9',
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
      };
        public static string GenerateRandomNumber(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
            
        }
 
 
        private void AddUser(string Userpassword)
        {
            string path = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry entryPC = new DirectoryEntry(path);
            DirectoryEntry obUser = entryPC.Children.Add("nginx", "User");
            obUser.Properties["Description"].Add("这是一个nginx启动账户,请勿删除");
            obUser.Properties["FullName"].Add("nginx");
            object obRet = obUser.Invoke("SetPassword", Userpassword);
            obUser.CommitChanges();
        }
 
        static void AddSecurityControll2Folder(string dirPath)
        {
            //获取文件夹信息
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            //获得该文件夹的所有访问权限
            System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
            //设定文件ACL继承
            InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            //添加administrators用户组的访问权限规则 完全控制权限
            FileSystemAccessRule administratorsFileSystemAccessRule = new FileSystemAccessRule("administrators", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            //添加nginx用户组的访问权限规则 完全控制权限
            FileSystemAccessRule nginxFileSystemAccessRule = new FileSystemAccessRule("nginx", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            bool isModified = false;
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, administratorsFileSystemAccessRule, out isModified); dirSecurity.ModifyAccessRule(AccessControlModification.Add, nginxFileSystemAccessRule, out isModified);
            //设置访问权限
            dir.SetAccessControl(dirSecurity);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string dirPath = "C:\\Users\\MaZi\\Desktop\\test";
            string Userpassword = GenerateRandomNumber(8);
            AddUser(Userpassword);
            MessageBox.Show("用户添加成功,用户名为:\"nginx\" 密码为:" + Userpassword);
            AddSecurityControll2Folder(dirPath);
            MessageBox.Show("已添加C:\\Users\\MaZi\\Desktop\\test文件夹添加了administrators及nginx的完全控制权限");
 
 
 
        }
 
    }
}
技术小学生微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://blog.tag.gg/showinfo-23-346-0.html
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
上一篇:C#随机生成一个数字+字母大小写的8位随机字符
下一篇:C#在防火墙中放行某端口方法