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));
48 public Task<ISystemIdentity> CreateSystemIdentity(
User user, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
51 throw new ArgumentNullException(nameof(user));
54 throw new InvalidOperationException(
"User's SystemIdentifier must not be null!");
56 PrincipalContext pc = null;
57 UserPrincipal principal = null;
59 bool TryGetPrincipalFromContextType(ContextType contextType)
63 pc =
new PrincipalContext(contextType);
64 cancellationToken.ThrowIfCancellationRequested();
67 catch (OperationCanceledException)
73 logger.LogWarning(
"Error loading user for context type {0}! Exception: {1}", contextType, e);
77 if (principal == null)
80 cancellationToken.ThrowIfCancellationRequested();
84 return principal != null;
87 if (!TryGetPrincipalFromContextType(ContextType.Machine) && !TryGetPrincipalFromContextType(ContextType.Domain))
91 cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
94 public Task<ISystemIdentity> CreateSystemIdentity(
string username,
string password, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
97 throw new ArgumentNullException(nameof(username));
99 throw new ArgumentNullException(nameof(password));
101 var originalUsername = username;
102 GetUserAndDomainName(originalUsername, out username, out var domainName);
107 logger.LogTrace(
"Invalid system identity/password combo for username {0}!", originalUsername);
111 logger.LogTrace(
"Authenticated username {0} using system identity!", originalUsername);
113 using (var handle =
new SafeAccessTokenHandle(token))
115 }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
ISystemIdentity for windows systems
Use server authentication
Represents a user on the current global::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 Windows 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