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;
36 this.identity = identity ??
throw new ArgumentNullException(nameof(identity));
45 this.userPrincipal = userPrincipal ??
throw new ArgumentNullException(nameof(userPrincipal));
55 var context = userPrincipal.Context;
56 userPrincipal.Dispose();
68 var newIdentity =
new WindowsIdentity(identity.Token);
73 throw new InvalidOperationException(
"Cannot clone a UserPrincipal based WindowsSystemIdentity!");
77 public Task RunImpersonated(Action action, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
80 throw new ArgumentNullException(nameof(action));
82 throw new InvalidOperationException(
"Impersonate using a UserPrincipal based WindowsSystemIdentity!");
83 WindowsIdentity.RunImpersonated(identity.AccessToken, action);
84 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
ISystemIdentity for windows systems
Use server authentication
Represents a user on the current 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