/// <summary>
        ///     Set the <see cref="HttpClient" /> to make requests using the specified user.
        /// </summary>
        /// <param name="client">The client that should start making user-faked requests.</param>
        /// <param name="credentials">The credentials of the user that will be logged in.</param>
        /// <returns>The <see cref="HttpClient" /> that was passed in.</returns>
        public static HttpClient SetWindowsAuthHeader(this HttpClient client, NetworkCredential credentials)
        {
            Guid guid = WindowsIdentityFactory.GetTokenForCredentials(credentials);

            // Set a request header that our authentication handler will use to create a ClaimsPrincipal.
            client.DefaultRequestHeaders.Add(WindowsAuthenticationHandler.CredentialKeyHeader, guid.ToString());

            return(client);
        }
Example #2
0
        public Task <AuthenticateResult> AuthenticateAsync()
        {
            if (_context.Request.Headers.Keys.Contains(CredentialKeyHeader))
            {
                string          credentialGuidString = _context.Request.Headers[CredentialKeyHeader].First();
                Guid            credentialKey        = Guid.Parse(credentialGuidString);
                WindowsIdentity identity             = WindowsIdentityFactory.LogInAs(credentialKey);

                var user = new WindowsPrincipal(identity);

                // Ensure that the identity is disposed of when the request is done
                // to avoid leaking unmanaged handles.
                _context.Response.RegisterForDispose(identity);

                return(Task.FromResult(
                           AuthenticateResult.Success(new AuthenticationTicket(user, AuthenticationScheme))));
            }

            return(Task.FromResult(AuthenticateResult.NoResult()));
        }