11# LocalSecurityEditor - .NET Library
22
3- .NET library for managing local security policy (User Rights Assignment).
3+ [ ![ NuGet Version] ( https://img.shields.io/nuget/v/LocalSecurityEditor )] ( https://www.nuget.org/packages/LocalSecurityEditor )
4+ [ ![ NuGet Downloads] ( https://img.shields.io/nuget/dt/LocalSecurityEditor )] ( https://www.nuget.org/packages/LocalSecurityEditor )
5+ ![ .NET Framework 4.7.2] ( https://img.shields.io/badge/.NET%20Framework-4.7.2-512BD4 )
6+ ![ .NET Standard 2.0] ( https://img.shields.io/badge/.NET%20Standard-2.0-512BD4 )
7+ ![ .NET (Windows) 8.0 | 9.0] ( https://img.shields.io/badge/.NET%20(Windows)-8.0%20%7C%209.0-512BD4 )
8+ ![ Platform Windows-only] ( https://img.shields.io/badge/Platform-Windows--only-blue )
49
10+ .NET library for managing local security policy (User Rights Assignment). This library powers PowerShell module scenarios and general .NET automation for querying and modifying User Rights Assignments (LSA policy).
511
6- ### Supported User Rights Assignement
12+
13+ ### Supported User Rights Assignment
714
815| ConstantName | Group Policy Setting |
916| ----------------------------------------- | ------------------------------------------------------------------ |
5360| SeSyncAgentPrivilege | Synchronize directory service data |
5461| SeTakeOwnershipPrivilege | Take ownership of files or other objects |
5562
56- ### Example Local Computer
63+ ### Example Local Computer (LSA wrapper)
5764
5865``` csharp
5966using System ;
@@ -64,7 +71,7 @@ namespace TestApp {
6471 static void Main () {
6572 string [] accounts ;
6673
67- Console .WriteLine (" [*] Accessing server - Displaying Current " );
74+ Console .WriteLine (" [*] Displaying current assignments (local) " );
6875
6976 using (LsaWrapper lsa = new LsaWrapper ()) {
7077 accounts = lsa .GetPrivileges (UserRightsAssignment .SeBatchLogonRight );
@@ -74,13 +81,13 @@ namespace TestApp {
7481 Console .WriteLine (account );
7582 }
7683
77- Console .WriteLine (" [*] Adding Account to the Server " );
84+ Console .WriteLine (" [*] Granting right to an account " );
7885
7986 using (LsaWrapper lsa = new LsaWrapper ()) {
8087 lsa .AddPrivileges (" EVOTEC\\ przemyslaw.klys" , UserRightsAssignment .SeBatchLogonRight );
8188 }
8289
83- Console .WriteLine (" [*] Accessing server - Displaying Current " );
90+ Console .WriteLine (" [*] Displaying current assignments (local) " );
8491
8592 using (LsaWrapper lsa = new LsaWrapper ()) {
8693 accounts = lsa .GetPrivileges (UserRightsAssignment .SeBatchLogonRight );
@@ -90,13 +97,13 @@ namespace TestApp {
9097 Console .WriteLine (account );
9198 }
9299
93- Console .WriteLine (" [*] Accessing server - Displaying Current " );
100+ Console .WriteLine (" [*] Removing a principal and listing again " );
94101
95102 using (LsaWrapper lsa = new LsaWrapper ()) {
96103 lsa .RemovePrivileges (" EVOTEC\\ przemyslaw.klys" , UserRightsAssignment .SeBatchLogonRight );
97104 }
98105
99- using (LsaWrapper lsa = new LsaWrapper (" " )) {
106+ using (LsaWrapper lsa = new LsaWrapper ()) {
100107 accounts = lsa .GetPrivileges (UserRightsAssignment .SeBatchLogonRight );
101108 }
102109
@@ -108,7 +115,7 @@ namespace TestApp {
108115}
109116```
110117
111- ### Example Remote Computer
118+ ### Example Remote Computer (LSA wrapper)
112119
113120``` csharp
114121using System ;
@@ -129,7 +136,7 @@ namespace TestApp {
129136 Console .WriteLine (account );
130137 }
131138
132- Console .WriteLine (" [*] Adding Account to the Server " );
139+ Console .WriteLine (" [*] Granting right to an account " );
133140
134141 using (LsaWrapper lsa = new LsaWrapper (" AD1" )) {
135142 lsa .AddPrivileges (" EVOTEC\\ przemyslaw.klys" , UserRightsAssignment .SeBatchLogonRight );
@@ -145,7 +152,7 @@ namespace TestApp {
145152 Console .WriteLine (account );
146153 }
147154
148- Console .WriteLine (" [*] Accessing AD1 server - Displaying Current " );
155+ Console .WriteLine (" [*] Removing the principal and listing again " );
149156
150157 using (LsaWrapper lsa = new LsaWrapper (" AD1" )) {
151158 lsa .RemovePrivileges (" EVOTEC\\ przemyslaw.klys" , UserRightsAssignment .SeBatchLogonRight );
@@ -159,6 +166,75 @@ namespace TestApp {
159166 Console .WriteLine (account );
160167 }
161168 }
162- }
163169}
164- ```
170+ }
171+ ```
172+
173+ ### Typed, OO API
174+
175+ ``` csharp
176+ using LocalSecurityEditor ;
177+
178+ // Enumerate all rights (local)
179+ var all = UserRights .Get ();
180+ foreach (var ura in all ) {
181+ Console .WriteLine ($" {ura .ShortName }: {ura .Count } principals" );
182+ }
183+
184+ // Lazy streaming
185+ foreach (var ura in new UserRights ().EnumerateLazy ()) {
186+ Console .WriteLine (ura );
187+ }
188+
189+ // Single right as typed object with principals
190+ var svc = UserRightsAssignment .SeServiceLogonRight .Get ();
191+ foreach (var p in svc .Principals ) {
192+ Console .WriteLine ($" {p .AccountName } -> {p .SidString }" );
193+ }
194+
195+ // Remote machine catalog
196+ var allRemote = UserRights .Get (" SERVER01" );
197+
198+ // Add/Remove/Set via fluent extensions
199+ UserRightsAssignment .SeBatchLogonRight .Add (@" DOMAIN\\svc_batch" );
200+ UserRightsAssignment .SeBatchLogonRight .Remove (@" DOMAIN\\old_user" );
201+ var result = UserRightsAssignment .SeDenyRemoteInteractiveLogonRight .Set (new []{ @" DOMAIN\\contractor1" , @" DOMAIN\\contractor2" });
202+ Console .WriteLine (result ); // e.g., SeDenyRemoteInteractiveLogonRight: +1 -0
203+
204+ // Batching with a manager (remote)
205+ using (var ur = new UserRights (" SERVER01" )) {
206+ ur .Add (UserRightsAssignment .SeBatchLogonRight , new [] { @" DOMAIN\\svc_batch" });
207+ var summary = ur .Set (UserRightsAssignment .SeServiceLogonRight , new [] { @" DOMAIN\\svc_svc" });
208+ Console .WriteLine (summary );
209+ }
210+
211+ ```
212+
213+ ### Async APIs
214+
215+ ``` csharp
216+ // Single right (local)
217+ var svc = await new UserRights ().GetStateAsync (UserRightsAssignment .SeServiceLogonRight , ct );
218+
219+ // Enumerate all (remote)
220+ var all = await new UserRights (" SERVER01" ).EnumerateAsync (ct );
221+
222+ // Fluent async extensions
223+ var svc2 = await UserRightsAssignment .SeServiceLogonRight .GetAsync (" SERVER01" , ct );
224+ ```
225+
226+ ### Thread Safety
227+
228+ - The library is safe to use from multiple tasks.
229+ - Internally it uses a reader–writer lock:
230+ - Reads may run in parallel; writes are exclusive; dispose is exclusive.
231+ - Prefer reusing a single ` UserRights ` instance for batching, or create per-task instances for isolation.
232+
233+ ### Generate service SIDs
234+
235+ ``` csharp
236+ string serviceName = " ADSync" ;
237+ string serviceExpectedSid = " S-1-5-80-3245704983-3664226991-764670653-2504430226-901976451" ;
238+ string serviceSid = NTService .GenerateSID (serviceName );
239+ Console .WriteLine ($" The SID for the service '{serviceName }' is: {serviceSid } {serviceExpectedSid } {(serviceSid == serviceExpectedSid )}" );
240+ ```
0 commit comments