2 using System.DirectoryServices.AccountManagement;
3 using System.Security.Principal;
5 using System.Threading.Tasks;
15 public string Uid => (userPrincipal?.Sid ?? identity.User).ToString();
18 public string Username => userPrincipal?.Name ?? identity.Name;
21 public bool CanCreateSymlinks => canCreateSymlinks ??
throw new NotSupportedException();
44 this.identity = identity ??
throw new ArgumentNullException(nameof(identity));
45 canCreateSymlinks =
new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
54 this.userPrincipal = userPrincipal ??
throw new ArgumentNullException(nameof(userPrincipal));
64 var context = userPrincipal.Context;
65 userPrincipal.Dispose();
76 var newIdentity =
new WindowsIdentity(identity.Token);
82 throw new InvalidOperationException(
"Cannot clone a UserPrincipal based WindowsSystemIdentity!");
86 public Task RunImpersonated(Action action, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
89 throw new ArgumentNullException(nameof(action));
91 throw new InvalidOperationException(
"Impersonate using a UserPrincipal based WindowsSystemIdentity!");
92 WindowsIdentity.RunImpersonated(identity.AccessToken, action);
93 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
ISystemIdentity for windows systems
Use server authentication
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.