Skip to content

Commit ce196a9

Browse files
authored
Add back HttpRequest.LogonUserIdentity (#528)
Previously, this had been added typed as IIdentity which was incorrect. This adds it back as WindowsIdentity and exposes it via a feature so it can be easily customized if needed. By default, it will use the current user and check if it has a WindowsIdentity and return that.
1 parent 5bbe34b commit ce196a9

File tree

16 files changed

+217
-73
lines changed

16 files changed

+217
-73
lines changed

Microsoft.AspNetCore.SystemWebAdapters.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.System
100100
EndProject
101101
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SystemWebAdapters.Apis.Tests", "test\Microsoft.AspNetCore.SystemWebAdapters.Apis.Tests\Microsoft.AspNetCore.SystemWebAdapters.Apis.Tests.csproj", "{E4D9A131-DC4E-403F-A10F-65F5E5E42475}"
102102
EndProject
103+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WindowsAuth", "samples\WindowsAuth\WindowsAuth.csproj", "{B5E840F8-2021-4175-BFBF-F9447506242E}"
104+
EndProject
103105
Global
104106
GlobalSection(SolutionConfigurationPlatforms) = preSolution
105107
Debug|Any CPU = Debug|Any CPU
@@ -222,6 +224,10 @@ Global
222224
{E4D9A131-DC4E-403F-A10F-65F5E5E42475}.Debug|Any CPU.Build.0 = Debug|Any CPU
223225
{E4D9A131-DC4E-403F-A10F-65F5E5E42475}.Release|Any CPU.ActiveCfg = Release|Any CPU
224226
{E4D9A131-DC4E-403F-A10F-65F5E5E42475}.Release|Any CPU.Build.0 = Release|Any CPU
227+
{B5E840F8-2021-4175-BFBF-F9447506242E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
228+
{B5E840F8-2021-4175-BFBF-F9447506242E}.Debug|Any CPU.Build.0 = Debug|Any CPU
229+
{B5E840F8-2021-4175-BFBF-F9447506242E}.Release|Any CPU.ActiveCfg = Release|Any CPU
230+
{B5E840F8-2021-4175-BFBF-F9447506242E}.Release|Any CPU.Build.0 = Release|Any CPU
225231
EndGlobalSection
226232
GlobalSection(SolutionProperties) = preSolution
227233
HideSolutionNode = FALSE
@@ -264,6 +270,7 @@ Global
264270
{FA39AC22-0725-4532-A682-B054ADA5BDA2} = {03E4CEAB-D845-402A-9311-F7390B24BA2A}
265271
{17055F45-E79A-41EF-825E-0B2211433729} = {A1BDA50C-D70B-416C-97F1-74B0649797C5}
266272
{E4D9A131-DC4E-403F-A10F-65F5E5E42475} = {A1BDA50C-D70B-416C-97F1-74B0649797C5}
273+
{B5E840F8-2021-4175-BFBF-F9447506242E} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
267274
EndGlobalSection
268275
GlobalSection(ExtensibilityGlobals) = postSolution
269276
SolutionGuid = {DABA3C65-9D74-4EB6-9B1C-730328710EAD}

samples/WindowsAuth/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Authentication.Negotiate;
2+
using Microsoft.AspNetCore.SystemWebAdapters;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
7+
.AddNegotiate();
8+
9+
var app = builder.Build();
10+
11+
app.UseAuthentication();
12+
13+
app.MapGet("/", (HttpContext context) =>
14+
{
15+
return new
16+
{
17+
User = context.User.Identity?.Name,
18+
Principal = context.AsSystemWeb().User.Identity?.Name,
19+
LogonUser = OperatingSystem.IsWindows() ? context.AsSystemWeb().Request.LogonUserIdentity?.Name : null,
20+
};
21+
});
22+
23+
app.Run();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": true,
5+
"anonymousAuthentication": false,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:50790",
8+
"sslPort": 44383
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5041",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7245;http://localhost:5041",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="8.0.7" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.CoreServices\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.csproj" />
15+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters\Microsoft.AspNetCore.SystemWebAdapters.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/Features/IPrincipalUserFeature.cs renamed to src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/Features/IRequestUserFeature.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@
55

66
using Microsoft.AspNetCore.Http.Features.Authentication;
77
using System.Diagnostics.CodeAnalysis;
8+
using System.Runtime.InteropServices;
9+
using System.Runtime.Versioning;
810
using System.Security.Claims;
911
using System.Security.Principal;
1012
using System.Web;
1113

1214
namespace Microsoft.AspNetCore.SystemWebAdapters.Features;
1315

1416
/// <summary>
15-
/// Represents the user as an <see cref="IPrincipal"/> as opposed to the in-built <see cref="IHttpAuthenticationFeature.User"/> which
16-
/// expects a <see cref="ClaimsPrincipal"/>.
17+
/// Represents the users that ASP.NET Framework used.
1718
/// </summary>
1819
[Experimental(Constants.ExperimentalFeatures.DiagnosticId)]
19-
public interface IPrincipalUserFeature
20+
public interface IRequestUserFeature
2021
{
22+
/// <summary>
23+
/// Gets or sets the user that corresponds to <see cref="HttpContext.User" />
24+
/// </summary>
2125
IPrincipal? User { get; set; }
26+
27+
/// <summary>
28+
/// Gets the logged on user that corresponds to <see cref="HttpRequest.LogonUserIdentity" />
29+
/// </summary>
30+
WindowsIdentity? LogonUserIdentity { get; }
2231
}
2332

2433
#endif

src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ internal HttpRequest() { }
439439
public bool IsLocal { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
440440
public bool IsSecureConnection { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
441441
public string this[string key] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
442+
public System.Security.Principal.WindowsIdentity LogonUserIdentity { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
442443
public System.Collections.Specialized.NameValueCollection Params { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
443444
public string Path { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
444445
public string PathInfo { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
@@ -483,6 +484,7 @@ public abstract partial class HttpRequestBase
483484
public virtual bool IsLocal { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
484485
public virtual bool IsSecureConnection { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
485486
public virtual string this[string key] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
487+
public virtual System.Security.Principal.WindowsIdentity LogonUserIdentity { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
486488
public virtual System.Collections.Specialized.NameValueCollection Params { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
487489
public virtual string Path { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
488490
public virtual string PathInfo { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
@@ -527,6 +529,7 @@ public partial class HttpRequestWrapper : System.Web.HttpRequestBase
527529
public override bool IsLocal { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
528530
public override bool IsSecureConnection { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
529531
public override string this[string key] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
532+
public override System.Security.Principal.WindowsIdentity LogonUserIdentity { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
530533
public override System.Collections.Specialized.NameValueCollection Params { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
531534
public override string Path { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
532535
public override string PathInfo { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }

src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,8 @@ public IHttpHandler? Handler
100100

101101
public IPrincipal User
102102
{
103-
get => Context.Features.Get<IPrincipalUserFeature>()?.User ?? Context.User;
104-
set
105-
{
106-
if (Context.Features.Get<IPrincipalUserFeature>() is { } feature)
107-
{
108-
feature.User = value;
109-
}
110-
else
111-
{
112-
var newFeature = new PrincipalUserFeature(Context) { User = value };
113-
114-
Context.Features.Set<IPrincipalUserFeature>(newFeature);
115-
Context.Features.Set<IHttpAuthenticationFeature>(newFeature);
116-
}
117-
}
103+
get => Context.Features.Get<IRequestUserFeature>()?.User ?? Context.User;
104+
set => Context.GetRequestUser().User = value;
118105
}
119106

120107
public HttpSessionState? Session => Context.Features.Get<ISessionStateFeature>()?.Session;

src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ internal HttpRequest(HttpRequestCore request)
5757

5858
public string CurrentExecutionFilePath => Request.HttpContext.Features.GetRequiredFeature<IHttpRequestPathFeature>().CurrentExecutionFilePath;
5959

60+
public WindowsIdentity? LogonUserIdentity => Request.HttpContext.GetRequestUser().LogonUserIdentity;
61+
6062
public NameValueCollection Headers => _headers ??= Request.Headers.ToNameValueCollection();
6163

6264
public Uri Url => new(Request.GetEncodedUrl());

0 commit comments

Comments
 (0)