1 using Microsoft.Extensions.Logging;
2 using Microsoft.Win32.SafeHandles;
4 using System.DirectoryServices.AccountManagement;
5 using System.Security.Principal;
7 using System.Threading.Tasks;
20 readonly ILogger<WindowsSystemIdentityFactory>
logger;
30 var splits = input.Split(
'\\');
31 username = splits.Length > 1 ? splits[1] : splits[0];
32 domainName = splits.Length > 1 ? splits[0] : null;
41 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
45 public Task<ISystemIdentity> CreateSystemIdentity(
User user, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
48 throw new ArgumentNullException(nameof(user));
51 throw new InvalidOperationException(
"User's SystemIdentifier must not be null!");
53 PrincipalContext pc = null;
54 UserPrincipal principal = null;
56 bool TryGetPrincipalFromContextType(ContextType contextType)
60 pc =
new PrincipalContext(contextType);
61 cancellationToken.ThrowIfCancellationRequested();
64 catch (OperationCanceledException)
70 logger.LogWarning(
"Error loading user for context type {0}! Exception: {1}", contextType, e);
74 if (principal == null)
77 cancellationToken.ThrowIfCancellationRequested();
80 return principal != null;
83 if (!TryGetPrincipalFromContextType(ContextType.Machine) && !TryGetPrincipalFromContextType(ContextType.Domain))
86 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
89 public Task<ISystemIdentity> CreateSystemIdentity(
string username,
string password, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
92 throw new ArgumentNullException(nameof(username));
94 throw new ArgumentNullException(nameof(password));
96 var originalUsername = username;
97 GetUserAndDomainName(originalUsername, out username, out var domainName);
102 logger.LogTrace(
"Failed to log in username {0}!", originalUsername);
106 logger.LogTrace(
"Successfully logged in username {0}!", originalUsername);
108 using (var handle =
new SafeAccessTokenHandle(token))
110 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
ISystemIdentity for windows systems
Use server authentication
Represents a user on the current System.Runtime.InteropServices.OSPlatform
readonly ILogger< WindowsSystemIdentityFactory > logger
The ILogger for the WindowsSystemIdentityFactory
WindowsSystemIdentityFactory(ILogger< WindowsSystemIdentityFactory > logger)
Construct a WindowsSystemIdentityFactory
ISystemIdentityFactory for windows systems. Uses long running tasks due to potential networked domain...
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 ...
Factory for ISystemIdentitys
Native methods used by the code
static void GetUserAndDomainName(string input, out string username, out string domainName)
Extract the username and domain name from a string in the format "username\\domainname" ...
string SystemIdentifier
The SID/UID of the User on Windows/POSIX respectively