tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
WindowsSystemIdentity.cs
Go to the documentation of this file.
1using System;
2using System.DirectoryServices.AccountManagement;
3using System.Runtime.Versioning;
4using System.Security.Principal;
5using System.Threading;
6using System.Threading.Tasks;
7
9
11{
15 [SupportedOSPlatform("windows")]
17 {
19 public string Uid => (userPrincipal?.Sid ?? identity!.User!).ToString(); // we kno user isn't null because it can only be the case when anonymous (checked in this constructor)
20
22 public string Username => userPrincipal?.Name ?? identity!.Name;
23
26
28 public bool IsSuperUser => isAdmin ?? throw new NotSupportedException();
29
33 readonly WindowsIdentity? identity;
34
38 readonly UserPrincipal? userPrincipal;
39
43 readonly bool? isAdmin;
44
49 public WindowsSystemIdentity(WindowsIdentity identity)
50 {
51 this.identity = identity ?? throw new ArgumentNullException(nameof(identity));
52 if (identity.IsAnonymous)
53 throw new ArgumentException($"Cannot use anonymous {nameof(WindowsIdentity)} as a {nameof(WindowsSystemIdentity)}!", nameof(identity));
54
55 isAdmin = new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
56 }
57
63 {
64 this.userPrincipal = userPrincipal ?? throw new ArgumentNullException(nameof(userPrincipal));
65 }
66
68 public void Dispose()
69 {
70 if (identity != null)
71 identity.Dispose();
72 else
73 {
74 var context = userPrincipal!.Context;
75 userPrincipal.Dispose();
76 context.Dispose();
77 }
78 }
79
82 {
83 if (identity != null)
84 {
85 // var newIdentity = (WindowsIdentity)identity.Clone(); //doesn't work because of https://github.com/dotnet/corefx/issues/31841
86 var newIdentity = new WindowsIdentity(identity.Token); // the handle is cloned internally
87
88 return new WindowsSystemIdentity(newIdentity);
89 }
90
91 // can't clone a UP, shouldn't be trying to anyway, cloning is for impersonation
92 throw new NotSupportedException("Cannot clone a UserPrincipal based WindowsSystemIdentity!");
93 }
94
96 public Task RunImpersonated(Action action, CancellationToken cancellationToken) => Task.Factory.StartNew(
97 () =>
98 {
99 ArgumentNullException.ThrowIfNull(action);
100 if (identity == null)
101 throw new NotSupportedException("Impersonate using a UserPrincipal based WindowsSystemIdentity!");
102 WindowsIdentity.RunImpersonated(identity.AccessToken, action);
103 },
104 cancellationToken,
106 TaskScheduler.Current);
107 }
108}
IIOManager that resolves paths to Environment.CurrentDirectory.
const TaskCreationOptions BlockingTaskCreationOptions
The TaskCreationOptions used to spawn Tasks for potentially long running, blocking operations.
WindowsSystemIdentity(WindowsIdentity identity)
Initializes a new instance of the WindowsSystemIdentity class.
readonly? UserPrincipal userPrincipal
The UserPrincipal for the WindowsSystemIdentity.
WindowsSystemIdentity(UserPrincipal userPrincipal)
Initializes a new instance of the WindowsSystemIdentity class.
ISystemIdentity Clone()
Clone the ISystemIdentity creating another copy that must have IDisposable.Dispose called on it....
Task RunImpersonated(Action action, CancellationToken cancellationToken)
Runs a given action in the context of the ISystemIdentity.A Task representing the running operation.
bool CanCreateSymlinks
If this system identity has permissions to create symlinks.
readonly? bool isAdmin
Backing field for IsSuperUser.
readonly? WindowsIdentity identity
The WindowsIdentity for the WindowsSystemIdentity.
bool IsSuperUser
Is this identity a SuperUser for the OS. See Administrator on Windows or root on Linux.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.