27 readonly ILogger<WindowsSystemIdentityFactory>
logger;
37 var splits = input.Split(
'\\');
38 username = splits.Length > 1 ? splits[1] : splits[0];
39 domainName = splits.Length > 1 ? splits[0] :
null;
48 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
58 ArgumentNullException.ThrowIfNull(user);
60 if (user.SystemIdentifier ==
null)
61 throw new InvalidOperationException(
"User's SystemIdentifier must not be null!");
63 PrincipalContext? pc =
null;
66 bool TryGetPrincipalFromContextType(ContextType contextType, [NotNullWhen(
true)] out UserPrincipal? principal)
71 pc = domainName !=
null
72 ?
new PrincipalContext(contextType, domainName)
73 :
new PrincipalContext(contextType);
74 cancellationToken.ThrowIfCancellationRequested();
75 principal = UserPrincipal.FindByIdentity(pc, user.SystemIdentifier);
77 catch (OperationCanceledException)
85 "Error loading user for context type {contextType} and principal \"{domainName}\"!",
91 if (principal ==
null)
94 cancellationToken.ThrowIfCancellationRequested();
98 return principal !=
null;
101 if (!TryGetPrincipalFromContextType(ContextType.Machine, out var principal) && !TryGetPrincipalFromContextType(ContextType.Domain, out principal))
107 TaskScheduler.Current);
110 public Task<ISystemIdentity?>
CreateSystemIdentity(
string username,
string password, CancellationToken cancellationToken) => Task.Factory.StartNew(
113 ArgumentNullException.ThrowIfNull(username);
114 ArgumentNullException.ThrowIfNull(password);
116 var originalUsername = username;
122 logger.LogTrace(
"Invalid system identity/password combo for username {0}!", originalUsername);
126 logger.LogTrace(
"Authenticated username {0} using system identity!", originalUsername);
129 using var handle =
new SafeAccessTokenHandle(token);
131 new WindowsIdentity(handle.DangerousGetHandle()));
135 TaskScheduler.Current);
static bool LogonUser(string lpszUsername, string? lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken)
See https://msdn.microsoft.com/en-us/library/windows/desktop/aa378184(v=vs.85).aspx.