2015-06-19 31 views
0

在下面的功能,在此行中我使用的特定語言的名稱爲「用戶」:用戶帳戶添加到用戶組而不管操作系統語言

GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users"); 

我該怎麼辦呢特定語言知道的?。

我的意思是,例如,在我的語言西班牙語中,組名稱是「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; 
    } 

} 

回答

5

您可以使用一個衆所周知的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);