博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NET C#创建WINDOWS系统用户
阅读量:6887 次
发布时间:2019-06-27

本文共 1768 字,大约阅读时间需要 5 分钟。

原文:

 

/前提是当前用户有相应的权限

/WinNT用户管理

using System;
using System.DirectoryServices; 
namespace Host.AdminManager.Inc
{
public class WindwosUser
{
//创建NT用户
//传入参数:Username要创建的用户名,Userpassword用户密码,Path主文件夹路径
public static bool CreateNTUser(string Username,string Userpassword,string Path)
{
DirectoryEntry obDirEntry
= null;
try
{
obDirEntry
= new DirectoryEntry("WinNT://" + Environment.MachineName);

DirectoryEntry obUser = obDirEntry.Children.Add(Username, "User"); //增加用户名

obUser.Properties["FullName"].Add(Username); //用户全称
obUser.Invoke("SetPassword", Userpassword); //用户密码
obUser.Invoke("Put", "Description","Test User from .NET");//用户详细描述
//obUser.Invoke("Put","PasswordExpired",1); //用户下次登录需更改密码
obUser.Invoke("Put","UserFlags",66049); //密码永不过期
obUser.Invoke("Put","HomeDirectory",Path); //主文件夹路径
obUser.CommitChanges();//保存用户
DirectoryEntry grp = obDirEntry.Children.Find("Users", "group");//Users组
if(grp.Name!="")
{
grp.Invoke(
"Add",obUser.Path.ToString());//将用户添加到某组
}
return true;
}
catch
{
return false;
}
}
//删除NT用户
//传入参数:Username用户名
public static bool DelNTUser(string Username)
{
try
{
DirectoryEntry obComputer
= new DirectoryEntry("WinNt://" + Environment.MachineName);//获得计算机实例
DirectoryEntry obUser = obComputer.Children.Find(Username,"User");//找得用户
obComputer.Children.Remove(obUser);//删除用户
return true;
}
catch
{
return false;
}
}
//修改NT用户密码
//传入参数:Username用户名,Userpassword用户新密码
public static bool InitNTPwd(string Username,string Userpassword)
{
try
{
DirectoryEntry obComputer
= new DirectoryEntry("WinNt://" + Environment.MachineName);
DirectoryEntry obUser
= obComputer.Children.Find(Username,"User");
obUser.Invoke(
"SetPassword", Userpassword);
obUser.CommitChanges();
obUser.Close();
obComputer.Close();
return true;
}
catch
{
return false;
}
}
}
}

 

转载地址:http://rjabl.baihongyu.com/

你可能感兴趣的文章
JUnit4 中@AfterClass @BeforeClass @after @before的区别对比
查看>>
jquery中的ajax参数说明
查看>>
Sublime Text2的常用技巧总结(更新中...)
查看>>
99%的人都理解错了HTTP中GET与POST的区别
查看>>
spring boot 配置mybatis plus 控制台打印sql
查看>>
XAML Namespace http://schemas.microsoft.com/expression/blend/2008 is not resolved
查看>>
Windows系统安装Apache-tomacat
查看>>
补习系列(11)-springboot 文件上传原理
查看>>
《用正确的方法解决问题100%》读书笔记
查看>>
CodeChef March Challenge 2019题解
查看>>
STL容器底层数据结构的实现
查看>>
Web设计的Ruby on Rails 第2章 变量、数组、散列表
查看>>
关于提升自己
查看>>
python基础
查看>>
权限管理--通用
查看>>
python爬虫爬取赶集网数据
查看>>
微信公众平台开发(系列教程)
查看>>
CentOS中配置Kafka集群
查看>>
Android IOS WebRTC 音视频开发总结(六六)-- 三个角度分析美女视频直播这个行业...
查看>>
ExpandableListView视图树简单应用
查看>>