tgstation-server 5.12.7
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();
20
22 public string Username => userPrincipal?.Name ?? identity.Name;
23
25 public bool CanCreateSymlinks => canCreateSymlinks ?? throw new NotSupportedException();
26
30 readonly WindowsIdentity identity;
31
35 readonly UserPrincipal userPrincipal;
36
40 readonly bool? canCreateSymlinks;
41
46 public WindowsSystemIdentity(WindowsIdentity identity)
47 {
48 this.identity = identity ?? throw new ArgumentNullException(nameof(identity));
49 canCreateSymlinks = new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
50 }
51
57 {
58 this.userPrincipal = userPrincipal ?? throw new ArgumentNullException(nameof(userPrincipal));
59 }
60
62 public void Dispose()
63 {
64 if (identity != null)
65 identity.Dispose();
66 else
67 {
68 var context = userPrincipal.Context;
69 userPrincipal.Dispose();
70 context.Dispose();
71 }
72 }
73
76 {
77 if (identity != null)
78 {
79 // var newIdentity = (WindowsIdentity)identity.Clone(); //doesn't work because of https://github.com/dotnet/corefx/issues/31841
80 var newIdentity = new WindowsIdentity(identity.Token); // the handle is cloned internally
81
82 return new WindowsSystemIdentity(newIdentity);
83 }
84
85 // can't clone a UP, shouldn't be trying to anyway, cloning is for impersonation
86 throw new InvalidOperationException("Cannot clone a UserPrincipal based WindowsSystemIdentity!");
87 }
88
90 public Task RunImpersonated(Action action, CancellationToken cancellationToken) => Task.Factory.StartNew(
91 () =>
92 {
93 ArgumentNullException.ThrowIfNull(action);
94 if (identity == null)
95 throw new InvalidOperationException("Impersonate using a UserPrincipal based WindowsSystemIdentity!");
96 WindowsIdentity.RunImpersonated(identity.AccessToken, action);
97 },
98 cancellationToken,
100 TaskScheduler.Current);
101 }
102}
IIOManager that resolves paths to Environment.CurrentDirectory.
const TaskCreationOptions BlockingTaskCreationOptions
The TaskCreationOptions used to spawn Tasks for potentially long running, blocking operations.
readonly WindowsIdentity identity
The WindowsIdentity for the WindowsSystemIdentity.
WindowsSystemIdentity(WindowsIdentity identity)
Initializes a new instance of the WindowsSystemIdentity class.
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...
readonly UserPrincipal userPrincipal
The UserPrincipal for the WindowsSystemIdentity.
bool CanCreateSymlinks
If this system identity has permissions to create symlinks.
readonly? bool canCreateSymlinks
Backing field for CanCreateSymlinks.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.