-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Use ImpersonationToken for IIS Windows Auth #58041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Security.Principal; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities; | ||
| using Microsoft.AspNetCore.Server.IntegrationTesting; | ||
|
|
@@ -58,4 +60,51 @@ public async Task WindowsAuthTest(TestVariant variant) | |
| Assert.StartsWith("Windows:", responseText); | ||
| Assert.Contains(Environment.UserName, responseText); | ||
| } | ||
|
|
||
| [ConditionalTheory] | ||
| [RequiresIIS(IISCapability.WindowsAuthentication)] | ||
| [MemberData(nameof(TestVariants))] | ||
| public async Task WindowsAuthWithImpersonationLevelTest(TestVariant variant) | ||
| { | ||
| var deploymentParameters = Fixture.GetBaseDeploymentParameters(variant); | ||
| deploymentParameters.SetAnonymousAuth(enabled: false); | ||
| deploymentParameters.SetWindowsAuth(); | ||
|
|
||
| // The default in hosting sets windows auth to true. | ||
| var deploymentResult = await DeployAsync(deploymentParameters); | ||
|
|
||
| var impersonationLevels = new TokenImpersonationLevel[] | ||
| { | ||
| TokenImpersonationLevel.None, | ||
| TokenImpersonationLevel.Identification, | ||
| TokenImpersonationLevel.Impersonation, | ||
| TokenImpersonationLevel.Delegation, | ||
| TokenImpersonationLevel.Anonymous | ||
| }; | ||
|
|
||
| foreach (var impersonationLevel in impersonationLevels) | ||
| { | ||
| // TokenImpersonationLevel is not supported by HttpClient so we need to use HttpWebRequest to test it. | ||
| #pragma warning disable SYSLIB0014 // Type or member is obsolete | ||
| var request = HttpWebRequest.CreateHttp($"{deploymentResult.HttpClient.BaseAddress}Auth"); | ||
| #pragma warning restore SYSLIB0014 // Type or member is obsolete | ||
| request.ImpersonationLevel = impersonationLevel; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to say ImpersonationLevel isn't supported, but went to check the source code in case anything has changed and was surprised to see that they use reflection to enable it for HttpWebRequest, even though HttpClientHandler/SocketsHttpHandler don't expose the api. Time to open an issue as this is an important missing feature that WCF needs. |
||
| request.Method = "GET"; | ||
| request.UseDefaultCredentials = true; | ||
|
|
||
| using var response = request.GetResponse(); | ||
| using var reader = new StreamReader(response.GetResponseStream()); | ||
| var responseText = await reader.ReadToEndAsync(); | ||
|
|
||
| try | ||
| { | ||
| Assert.StartsWith("Windows:", responseText); | ||
| Assert.Contains(Environment.UserName, responseText); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Assert.Fail($"'TokenImpersonationLevel.{impersonationLevel}' failed with: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆