Guest User

Untitled

a guest
Sep 3rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. Trying my first steps in c# : create local user account, check if os is english, german or something other, choose groupname and move into it [closed]
  2. using System;
  3. using System.DirectoryServices;
  4. using System.Globalization;
  5. using System.Security.Permissions;
  6. using System.Threading;
  7.  
  8. [assembly:SecurityPermission( SecurityAction.RequestMinimum, ControlThread = true )]
  9.  
  10. class CreateUser
  11.  
  12. {
  13. static void Main(string[] args)
  14. {
  15. try
  16. {
  17.  
  18. // Parameter for the account
  19. string s_Username = "EmpService";
  20. string s_Password = "password";
  21. string s_Description = "Empirum Service User for Management";
  22.  
  23. // string s_Group1 = "Administrators";
  24. // string s_Group2 = "Administratoren";
  25.  
  26.  
  27. // Check if Useraccount allready exists
  28. private bool Check(string s_username)
  29. {
  30. DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
  31.  
  32. foreach (DirectoryEntry child in entry.Children)
  33. {
  34. if (child.SchemaClassName == "User")
  35. {
  36. if (child.Name == s_username)
  37. {
  38. return true;
  39. Console.WriteLine("User Account allready exists.");
  40. {
  41. }
  42. }
  43. return false;
  44.  
  45.  
  46. public static CultureInfo CurrentCulture { get; };
  47. if (CultureInfo.CurrentCulture.Name.StartsWith("de-"))
  48.  
  49.  
  50. {
  51. string s_group= "Administratoren";
  52. Console.WriteLine (" OS Language is GERMAN.");
  53. }
  54. else
  55. {
  56. string s_group = "Administrators";
  57. Console.WriteLine("OS Language is NOT German.");
  58. }
  59.  
  60. // Create User Account
  61. DirectoryEntry NewUser = AD.Children.Add(s_Username, "user");
  62.  
  63. // Useraccount-Settings
  64. NewUser.Invoke("SetPassword", new object[] { s_Password });
  65. NewUser.Invoke("Put", new object[] { "Description", s_Description });
  66.  
  67. // Add User to a Group, for example
  68. // Check if OS is German or English
  69. // if OS is english, use "Administrators" as group
  70. // if OS is german, use "Administratoren" as group
  71.  
  72.  
  73.  
  74. // grp = AD.Children.Find("Guests", "group");
  75. // if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
  76. // needs to be completed
  77.  
  78. // SuccessMessage
  79. NewUser.CommitChanges();
  80. Console.WriteLine("EmpServiceBenutzer erfolgreich angelegt.");
  81. }
  82. catch (Exception err)
  83. {
  84. // ErrorMessage
  85. Console.WriteLine(err.Message);
  86. }}
  87. ;
  88. }
Add Comment
Please, Sign In to add comment