-
Notifications
You must be signed in to change notification settings - Fork 68
Add back HttpRequest.LogonUserIdentity #528
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using Microsoft.AspNetCore.Authentication.Negotiate; | ||
| using Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme) | ||
| .AddNegotiate(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseAuthentication(); | ||
|
|
||
| app.MapGet("/", (HttpContext context) => | ||
| { | ||
| return new | ||
| { | ||
| User = context.User.Identity?.Name, | ||
| Principal = context.AsSystemWeb().User.Identity?.Name, | ||
| LogonUser = OperatingSystem.IsWindows() ? context.AsSystemWeb().Request.LogonUserIdentity?.Name : null, | ||
| }; | ||
| }); | ||
|
|
||
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": true, | ||
| "anonymousAuthentication": false, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:50790", | ||
| "sslPort": 44383 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5041", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7245;http://localhost:5041", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="8.0.7" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.CoreServices\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.csproj" /> | ||
| <ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters\Microsoft.AspNetCore.SystemWebAdapters.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
src/Microsoft.AspNetCore.SystemWebAdapters/Internal/PrincipalUserFeature.cs
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
src/Microsoft.AspNetCore.SystemWebAdapters/Internal/RequestUserExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Security.Claims; | ||
| using System.Security.Principal; | ||
| using Microsoft.AspNetCore.Http.Features.Authentication; | ||
| using Microsoft.AspNetCore.SystemWebAdapters.Features; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| internal static partial class RequestUserExtensions | ||
| { | ||
| public static IRequestUserFeature GetRequestUser(this HttpContextCore context) | ||
| { | ||
| if (context.Features.Get<IRequestUserFeature>() is { } existing) | ||
| { | ||
| return existing; | ||
| } | ||
|
|
||
| var newFeature = new RequestUserFeature(context) { User = context.User }; | ||
|
|
||
| context.Features.Set<IRequestUserFeature>(newFeature); | ||
| context.Features.Set<IHttpAuthenticationFeature>(newFeature); | ||
|
|
||
| return newFeature; | ||
| } | ||
|
|
||
| private sealed partial class RequestUserFeature : IRequestUserFeature, IHttpAuthenticationFeature | ||
| { | ||
| private readonly ILogger _logger; | ||
|
|
||
| public RequestUserFeature(HttpContextCore context) | ||
| { | ||
| var logger = (ILogger?)context.RequestServices?.GetService<ILoggerFactory>()?.CreateLogger<RequestUserFeature>(); | ||
|
|
||
| _logger = logger ?? NullLogger.Instance; | ||
|
|
||
| User = context.User; | ||
| } | ||
|
|
||
| [LoggerMessage(0, LogLevel.Debug, "A custom principal {PrincipalType} is being used and should be replaced with a ClaimsPrincipal derived type.")] | ||
| private partial void LogNonClaimsPrincipal(Type principalType); | ||
|
|
||
| public IPrincipal? User { get; set; } | ||
|
|
||
| WindowsIdentity? IRequestUserFeature.LogonUserIdentity => User?.Identity as WindowsIdentity; | ||
|
|
||
| ClaimsPrincipal? IHttpAuthenticationFeature.User | ||
| { | ||
| get => GetOrCreateClaims(User); | ||
| set => User = value; | ||
| } | ||
|
|
||
| private ClaimsPrincipal? GetOrCreateClaims(IPrincipal? principal) | ||
| { | ||
| if (principal is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (principal is ClaimsPrincipal claimsPrincipal) | ||
| { | ||
| return claimsPrincipal; | ||
| } | ||
|
|
||
| LogNonClaimsPrincipal(principal.GetType()); | ||
|
|
||
| return new ClaimsPrincipal(principal); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.