C# 将用户帐户添加到用户组,而不考虑O.S语言
在下面的函数中,我在这一行中使用特定于语言的名称“Users”: 我如何做到这一点语言特定意识 我的意思是,例如,在我的西班牙语中,组名是“Usuarios”,因此函数将抛出异常,因为找不到英文名称 我希望能够将用户添加到管理员、标准用户和来宾,而不必担心机器当前的文化语言。可能吗C# 将用户帐户添加到用户组,而不考虑O.S语言,c#,.net,account,directoryservices,user-accounts,C#,.net,Account,Directoryservices,User Accounts,在下面的函数中,我在这一行中使用特定于语言的名称“Users”: 我如何做到这一点语言特定意识 我的意思是,例如,在我的西班牙语中,组名是“Usuarios”,因此函数将抛出异常,因为找不到英文名称 我希望能够将用户添加到管理员、标准用户和来宾,而不必担心机器当前的文化语言。可能吗 职能: public static bool CreateLocalWindowsAccount(string username, string password, string displayName, stri
职能:
public static bool CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
{
try
{
PrincipalContext context = new PrincipalContext(ContextType.Machine);
UserPrincipal user = new UserPrincipal(context);
user.SetPassword(password);
user.DisplayName = displayName;
user.Name = username;
user.Description = description;
user.UserCannotChangePassword = canChangePwd;
user.PasswordNeverExpires = pwdExpires;
user.Save();
//now add user to "Users" group so it displays in Control Panel
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users");
group.Members.Add(user);
group.Save();
return true;
}
catch (Exception ex)
{
LogMessageToFile("error msg" + ex.Message);
return false;
}
}
可以使用已知的SID指定要检索的组。对于
用户
组,SID为S-1-5-32-545
,但通过使用枚举,可以避免将实际SID放入代码中:
var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var principal = GroupPrincipal.FindByIdentity(context, IdentityType.Sid, sid.Value);
var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var principal = GroupPrincipal.FindByIdentity(context, IdentityType.Sid, sid.Value);