使用C#实现Windows组和用户管理的示例代码
1、WindowsAccountHelper类实现
usingSystem;
usingSystem.Collections.Generic;
usingSystem.DirectoryServices.AccountManagement;
usingSystem.Linq;
publicclassWindowsAccountHelper
{
publicstaticstringLastErrorMsg{get;privateset;}
publicstaticListGetGroups()
{
vargroups=newList();
try
{
varcontext=newPrincipalContext(ContextType.Machine);
varqueryGroup=newGroupPrincipal(context);
varsearcher=newPrincipalSearcher(queryGroup);
searcher.FindAll().ToList().ForEach(t=>groups.Add(t.Name));
}
catch(Exception)
{
groups.Clear();
}
returngroups;
}
publicstaticListGetGroupUsers(stringgroupName)
{
vargroup=GetGroup(groupName);
returnGetGroupUsers(group);
}
publicstaticListGetGroupUsers(GroupPrincipalgroup)
{
varusers=newList();
if(group==null)
{
returnusers;
}
group.GetMembers().ToList().ForEach(t=>users.Add(t.Name));
returnusers;
}
publicstaticGroupPrincipalGetGroup(stringgroupName)
{
GroupPrincipalgroup=null;
try
{
varcontext=newPrincipalContext(ContextType.Machine);
varqueryGroup=newGroupPrincipal(context);
varsearcher=newPrincipalSearcher(queryGroup);
foreach(varprincipalinsearcher.FindAll())
{
vargroupPrincipal=(GroupPrincipal)principal;
if(groupPrincipal!=null&&groupPrincipal.Name.Equals(groupName))
{
group=groupPrincipal;
break;
}
}
}
catch(Exception)
{
//ignored
}
returngroup;
}
publicstaticGroupPrincipalCreateGroup(stringgroupName,stringdescription,boolisSecurityGroup)
{
GroupPrincipalgroup;
try
{
group=GetGroup(groupName);
if(group==null)
{
varcontext=newPrincipalContext(ContextType.Machine);
group=newGroupPrincipal(context)
{
Name=groupName,
Description=description,
IsSecurityGroup=isSecurityGroup,
GroupScope=GroupScope.Local
};
group.Save();
}
}
catch(Exceptione)
{
LastErrorMsg=e.Message;
group=null;
}
returngroup;
}
publicstaticboolDeleteGroup(stringgroupName)
{
vargroup=GetGroup(groupName);
if(group==null)
{
returntrue;
}
varret=true;
try
{
group.Delete();
}
catch(Exception)
{
ret=false;
}
returnret;
}
publicstaticboolCreateWindowsAccount(stringuserName,stringpassword,
stringdisplayName,stringdescription,boolcannotChangePassword,
boolpasswordNeverExpires,stringgroupName)
{
boolret;
try
{
varcontext=newPrincipalContext(ContextType.Machine);
vargroup=GroupPrincipal.FindByIdentity(context,groupName);
if(group==null)
{
returnfalse;
}
ret=CreateWindowsAccount(userName,password,displayName,
description,cannotChangePassword,passwordNeverExpires,group);
}
catch(Exception)
{
ret=false;
}
returnret;
}
publicstaticboolCreateWindowsAccount(stringuserName,stringpassword,
stringdisplayName,stringdescription,boolcannotChangePassword,
boolpasswordNeverExpires,GroupPrincipalgroup)
{
boolret;
try
{
if(group==null)
{
returnfalse;
}
varcontext=newPrincipalContext(ContextType.Machine);
varuser=UserPrincipal.FindByIdentity(context,userName)
??newUserPrincipal(context);
user.SetPassword(password);
user.DisplayName=displayName;
user.Name=userName;
user.Description=description;
user.UserCannotChangePassword=cannotChangePassword;
user.PasswordNeverExpires=passwordNeverExpires;
user.Save();
group.Members.Add(user);
group.Save();
ret=true;
}
catch(Exception)
{
ret=false;
}
returnret;
}
publicstaticboolDeleteWindowsAccount(ListuserNameList)
{
varret=true;
try
{
foreach(varuserNameinuserNameList)
{
varcontext=newPrincipalContext(ContextType.Machine);
varuser=UserPrincipal.FindByIdentity(context,userName);
user?.Delete();
}
}
catch(Exception)
{
ret=false;
}
returnret;
}
publicstaticboolChangeUserGroup(stringuserName,stringgroupName)
{
boolret;
try
{
varcontext=newPrincipalContext(ContextType.Machine);
vargroup=GroupPrincipal.FindByIdentity(context,groupName);
if(group==null)
{
returnfalse;
}
ret=ChangeUserGroup(userName,group);
}
catch(Exception)
{
ret=false;
}
returnret;
}
publicstaticboolChangeUserGroup(stringuserName,GroupPrincipalgroup)
{
boolret;
try
{
if(group==null)
{
returnfalse;
}
varcontext=newPrincipalContext(ContextType.Machine);
varuser=UserPrincipal.FindByIdentity(context,userName);
if(user==null)
{
returnfalse;
}
if(!group.Members.Contains(user))
{
group.Members.Add(user);
group.Save();
}
ret=true;
}
catch(Exception)
{
ret=false;
}
returnret;
}
publicstaticintUpdateGroupUsers(stringgroupName,ListuserNames,stringpassword="")
{
vargroup=CreateGroup(groupName,string.Empty,false);
if(group==null)
{
return0;
}
varuserNameList=newList();
userNameList.AddRange(userNames);
varaddedUsers=newList();
intgroupUserCount;
try
{
foreach(varprincipalingroup.GetMembers())
{
varuser=(UserPrincipal)principal;
if(user==null)
{
continue;
}
if(userNameList.Contains(user.Name))
{
//已有用户
addedUsers.Add(user.Name);
}
else
{
user.Delete();
}
}
//已有用户数
groupUserCount=addedUsers.Count;
//剩余的即为需要添加的用户集合
foreach(varuserNameinaddedUsers)
{
userNameList.Remove(userName);
}
//创建用户
foreach(varuserNameinuserNameList)
{
if(CreateWindowsAccount(userName,password,
userName,string.Empty,
false,false,group))
{
groupUserCount++;
}
}
}
catch(UnauthorizedAccessException)
{
groupUserCount=0;
}
returngroupUserCount;
}
}
2、使用示例
privateboolCreateGroupUsers(stringgroupName,ListwindowsUserList, stringpassword,intuserCount) { vargroup=WindowsAccountHelper.CreateGroup(groupName,string.Empty,true); if(group==null) { returnfalse; } varuserNames=WindowsAccountHelper.GetGroupUsers(group); foreach(varuserNameinWindowsUserList) { if(!userNames.Contains(userName)) { if(!WindowsAccountHelper.CreateWindowsAccount(userName,password, userName,string.Empty, false,false,group)) { returnfalse; } } } returntrue; }
以上就是使用C#实现Windows组和用户管理的示例代码的详细内容,更多关于C#实现Windows组和用户管理的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。