tgstation-server  4.4.0
The /tg/station 13 server suite
WindowsSystemIdentity.cs
Go to the documentation of this file.
1 using System;
2 using System.DirectoryServices.AccountManagement;
3 using System.Security.Principal;
4 using System.Threading;
5 using System.Threading.Tasks;
6 
7 namespace Tgstation.Server.Host.Security
8 {
13  {
15  public string Uid => (userPrincipal?.Sid ?? identity.User).ToString();
16 
18  public string Username => userPrincipal?.Name ?? identity.Name;
19 
21  public bool CanCreateSymlinks => canCreateSymlinks ?? throw new NotSupportedException();
22 
26  readonly WindowsIdentity identity;
27 
31  readonly UserPrincipal userPrincipal;
32 
36  readonly bool? canCreateSymlinks;
37 
42  public WindowsSystemIdentity(WindowsIdentity identity)
43  {
44  this.identity = identity ?? throw new ArgumentNullException(nameof(identity));
45  canCreateSymlinks = new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
46  }
47 
52  public WindowsSystemIdentity(UserPrincipal userPrincipal)
53  {
54  this.userPrincipal = userPrincipal ?? throw new ArgumentNullException(nameof(userPrincipal));
55  }
56 
58  public void Dispose()
59  {
60  if (identity != null)
61  identity.Dispose();
62  else
63  {
64  var context = userPrincipal.Context;
65  userPrincipal.Dispose();
66  context.Dispose();
67  }
68  }
69 
72  {
73  if (identity != null)
74  {
75  // var newIdentity = (WindowsIdentity)identity.Clone(); //doesn't work because of https://github.com/dotnet/corefx/issues/31841
76  var newIdentity = new WindowsIdentity(identity.Token); // the handle is cloned internally
77 
78  return new WindowsSystemIdentity(newIdentity);
79  }
80 
81  // can't clone a UP, shouldn't be trying to anyway, cloning is for impersonation
82  throw new InvalidOperationException("Cannot clone a UserPrincipal based WindowsSystemIdentity!");
83  }
84 
86  public Task RunImpersonated(Action action, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
87  {
88  if (action == null)
89  throw new ArgumentNullException(nameof(action));
90  if (identity == null)
91  throw new InvalidOperationException("Impersonate using a UserPrincipal based WindowsSystemIdentity!");
92  WindowsIdentity.RunImpersonated(identity.AccessToken, action);
93  }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
94  }
95 }
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform ...
WindowsSystemIdentity(WindowsIdentity identity)
Construct a WindowsSystemIdentity using a WindowsIdentity
WindowsSystemIdentity(UserPrincipal userPrincipal)
Construct a WindowsSystemIdentity using a UserPrincipal
ISystemIdentity Clone()
Clone the ISystemIdentity creating another copy that must have IDisposable.Dispose called on it ...
readonly WindowsIdentity identity
The WindowsIdentity for the WindowsSystemIdentity
readonly UserPrincipal userPrincipal
The UserPrincipal for the WindowsSystemIdentity
readonly bool canCreateSymlinks
Backing field for CanCreateSymlinks.