无论操作系统语言如何,都将用户帐户添加到用户组

Ele*_*ios 1 .net c# account directoryservices user-accounts

在下面的函数中,在这一行中,我使用特定于语言的名称" Users ":

GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users");
Run Code Online (Sandbox Code Playgroud)

我怎么能用语言特定的意识呢?

我的意思是,例如,在我的语言中,西班牙语,组名是" Usuarios ",因此该函数将抛出异常,因为找不到英文名称.

我希望能够将用户添加到Administrador,标准用户和来宾,而无需担心机器当前的文化语言.可能吗?


功能:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*age 6

您可以使用众所周知的SID指定要检索的组.对于Users组,SID是S-1-5-32-545使用WellKnownSidType枚举,您可以避免将实际的SID放入代码中:

var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var principal = GroupPrincipal.FindByIdentity(context, IdentityType.Sid, sid.Value);
Run Code Online (Sandbox Code Playgroud)