Skip to content

Commit ebec672

Browse files
committed
Revert "Expand ASP.NET Framework to Core migration" (#35768)
1 parent 01d2c30 commit ebec672

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+777
-1749
lines changed

.openpublishing.redirection.json

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,16 +1010,6 @@
10101010
"redirect_url": "/aspnet/core/migration/fx-to-core/systemweb-adapters",
10111011
"redirect_document_id": false
10121012
},
1013-
{
1014-
"source_path": "aspnetcore/migration/inc/unit-testing.md",
1015-
"redirect_url": "/aspnet/core/migration/fx-to-core/systemweb-adapters",
1016-
"redirect_document_id": false
1017-
},
1018-
{
1019-
"source_path": "aspnetcore/migration/fx-to-core/inc/unit-testing.md",
1020-
"redirect_url": "/aspnet/core/migration/fx-to-core/systemweb-adapters",
1021-
"redirect_document_id": false
1022-
},
10231013
{
10241014
"source_path": "aspnetcore/migration/inc/blazor.md",
10251015
"redirect_url": "/aspnet/core/migration/fx-to-core/inc/blazor",
@@ -1032,7 +1022,7 @@
10321022
},
10331023
{
10341024
"source_path": "aspnetcore/migration/inc/overview.md",
1035-
"redirect_url": "/aspnet/core/migration/fx-to-core/index",
1025+
"redirect_url": "/aspnet/core/migration/fx-to-core/inc/overview",
10361026
"redirect_document_id": false
10371027
},
10381028
{
@@ -1057,44 +1047,24 @@
10571047
},
10581048
{
10591049
"source_path": "aspnetcore/migration/inc/start.md",
1060-
"redirect_url": "/aspnet/core/migration/fx-to-core/start",
1050+
"redirect_url": "/aspnet/core/migration/fx-to-core/inc/start",
10611051
"redirect_document_id": false
10621052
},
10631053
{
1064-
"source_path": "aspnetcore/migration/inc/usage_guidance.md",
1065-
"redirect_url": "/aspnet/core/migration/fx-to-core/",
1054+
"source_path": "aspnetcore/migration/inc/unit-testing.md",
1055+
"redirect_url": "/aspnet/core/migration/fx-to-core/inc/unit-testing",
10661056
"redirect_document_id": false
10671057
},
10681058
{
1069-
"source_path": "aspnetcore/migration/fx-to-core/inc/usage_guidance.md",
1070-
"redirect_url": "/aspnet/core/migration/fx-to-core/",
1059+
"source_path": "aspnetcore/migration/inc/usage_guidance.md",
1060+
"redirect_url": "/aspnet/core/migration/fx-to-core/inc/usage_guidance",
10711061
"redirect_document_id": false
10721062
},
10731063
{
10741064
"source_path": "aspnetcore/migration/inc/wrapped.md",
10751065
"redirect_url": "/aspnet/core/migration/fx-to-core/inc/wrapped",
10761066
"redirect_document_id": false
10771067
},
1078-
{
1079-
"source_path": "aspnetcore/migration/fx-to-core/inc/remote-session.md",
1080-
"redirect_url": "/aspnet/core/migration/fx-to-core/areas/session#remote-app-session-state",
1081-
"redirect_document_id": false
1082-
},
1083-
{
1084-
"source_path": "aspnetcore/migration/fx-to-core/inc/remote-authentication.md",
1085-
"redirect_url": "/aspnet/core/migration/fx-to-core/areas/authentication#remote-authentication",
1086-
"redirect_document_id": false
1087-
},
1088-
{
1089-
"source_path": "aspnetcore/migration/fx-to-core/inc/session.md",
1090-
"redirect_url": "/aspnet/core/migration/fx-to-core/areas/session",
1091-
"redirect_document_id": false
1092-
},
1093-
{
1094-
"source_path": "aspnetcore/migration/fx-to-core/inc/wrapped.md",
1095-
"redirect_url": "/aspnet/core/migration/fx-to-core/areas/session#wrapped-aspnet-core-session-state",
1096-
"redirect_document_id": false
1097-
},
10981068
{
10991069
"source_path": "aspnetcore/security/blazor/server-side.md",
11001070
"redirect_url": "/aspnet/core/blazor/security/server/",

aspnetcore/migration/fx-to-core/areas/authentication.md

Lines changed: 0 additions & 374 deletions
This file was deleted.
Lines changed: 17 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,202 +1,31 @@
11
---
2-
title: Migrate from static ClaimsPrincipal access
2+
title: Migrate from ClaimsPrincipal.Current
33
author: mjrousos
4-
description: Learn how to migrate away from static ClaimsPrincipal access to retrieve the current authenticated user's identity and claims in ASP.NET Core.
4+
description: Learn how to migrate away from ClaimsPrincipal.Current to retrieve the current authenticated user's identity and claims in ASP.NET Core.
55
ms.author: wpickett
66
ms.custom: mvc
7-
ms.date: 07/17/2025
7+
ms.date: 03/26/2019
88
uid: migration/fx-to-core/areas/claimsprincipal-current
99
---
10-
# Migrate from static ClaimsPrincipal access
10+
# Migrate from ClaimsPrincipal.Current
1111

12-
The current ClaimsPrincipal is a fundamental component of authenticated web applications, providing access to the current user's identity and claims. When migrating from ASP.NET Framework to ASP.NET Core, accessing this presents unique challenges because the two frameworks have different approaches to user context management.
12+
In ASP.NET 4.x projects, it was common to use <xref:System.Security.Claims.ClaimsPrincipal.Current%2A?displayProperty=nameWithType> to retrieve the current authenticated user's identity and claims. In ASP.NET Core, this property is no longer set. Code that was depending on it needs to be updated to get the current authenticated user's identity through a different means.
1313

14-
## Why static ClaimsPrincipal migration is complex
14+
## Context-specific state instead of static state
1515

16-
ASP.NET Framework and ASP.NET Core take fundamentally different approaches to how the current user is accessed:
16+
When using ASP.NET Core, the values of both `ClaimsPrincipal.Current` and `Thread.CurrentPrincipal` aren't set. These properties both represent static state, which ASP.NET Core generally avoids. Instead, ASP.NET Core uses [dependency injection](xref:fundamentals/dependency-injection) (DI) to provide dependencies such as the current user's identity. Getting the current user's identity from DI is more testable, too, since test identities can be easily injected.
1717

18-
* **ASP.NET Framework** uses static properties like <xref:System.Security.Claims.ClaimsPrincipal.Current?displayProperty=nameWithType> and <xref:System.Threading.Thread.CurrentPrincipal?displayProperty=nameWithType> with automatic context management. These properties are interchangeable and both provide access to the current user's identity.
19-
* **ASP.NET Core** stores the current user in <xref:Microsoft.AspNetCore.Http.HttpContext.User?displayProperty=nameWithType> and avoids static state.
18+
## Retrieve the current user in an ASP.NET Core app
2019

21-
These differences mean you can't simply continue using static principal properties (<xref:System.Security.Claims.ClaimsPrincipal.Current?displayProperty=nameWithType> or <xref:System.Threading.Thread.CurrentPrincipal?displayProperty=nameWithType>) in ASP.NET Core without changes. By default, the static properties aren't set, and code depending on them needs to be updated to get the current authenticated user's identity through different means.
20+
There are several options for retrieving the current authenticated user's `ClaimsPrincipal` in ASP.NET Core in place of `ClaimsPrincipal.Current`:
2221

23-
## Migration strategies overview
22+
* **ControllerBase.User**. MVC controllers can access the current authenticated user with their <xref:Microsoft.AspNetCore.Mvc.ControllerBase.User%2A> property.
23+
* **HttpContext.User**. Components with access to the current `HttpContext` (middleware, for example) can get the current user's `ClaimsPrincipal` from <xref:Microsoft.AspNetCore.Http.HttpContext.User%2A?displayProperty=nameWithType>.
24+
* **Passed in from caller**. Libraries without access to the current `HttpContext` are often called from controllers or middleware components and can have the current user's identity passed as an argument.
25+
* **IHttpContextAccessor**. The project being migrated to ASP.NET Core may be too large to easily pass the current user's identity to all necessary locations. In such cases, <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> can be used as a workaround. `IHttpContextAccessor` is able to access the current `HttpContext` (if one exists). If DI is being used, see <xref:fundamentals/httpcontext>. A short-term solution to getting the current user's identity in code that hasn't yet been updated to work with ASP.NET Core's DI-driven architecture would be:
2426

25-
You have two main approaches for handling static principal access during migration:
27+
* Make `IHttpContextAccessor` available in the DI container by calling [AddHttpContextAccessor](https://github.com/aspnet/Hosting/issues/793) in `Startup.ConfigureServices`.
28+
* Get an instance of `IHttpContextAccessor` during startup and store it in a static variable. The instance is made available to code that was previously retrieving the current user from a static property.
29+
* Retrieve the current user's `ClaimsPrincipal` using `HttpContextAccessor.HttpContext?.User`. If this code is used outside of the context of an HTTP request, the `HttpContext` is null.
2630

27-
1. **Complete rewrite** - Rewrite all static principal access code to use ASP.NET Core's native patterns
28-
2. **System.Web adapters** - Use adapters to enable static access patterns during incremental migration
29-
30-
For most applications, migrating to ASP.NET Core's native ClaimsPrincipal access provides the best performance and maintainability. However, larger applications or those with extensive static principal usage may benefit from using System.Web adapters during incremental migration.
31-
32-
## Choose your migration approach
33-
34-
You have two main options for migrating static principal access from ASP.NET Framework to ASP.NET Core. Your choice depends on your migration timeline, whether you need to run both applications simultaneously, and how much code you're willing to rewrite.
35-
36-
### Quick decision guide
37-
38-
**Answer these questions to choose your approach:**
39-
40-
1. **Are you doing a complete rewrite or incremental migration?**
41-
* Complete rewrite → [Complete rewrite to ASP.NET Core patterns](#complete-rewrite-to-aspnet-core-patterns)
42-
* Incremental migration → Continue to question 2
43-
44-
2. **Do you have extensive static principal usage (<xref:System.Security.Claims.ClaimsPrincipal.Current?displayProperty=nameWithType> or <xref:System.Threading.Thread.CurrentPrincipal?displayProperty=nameWithType>) across shared libraries?**
45-
* Yes, lots of shared code → [System.Web adapters](#systemweb-adapters)
46-
* No, isolated static principal usage → [Complete rewrite to ASP.NET Core patterns](#complete-rewrite-to-aspnet-core-patterns)
47-
48-
### Migration approaches comparison
49-
50-
| Approach | Code Changes | Performance | Shared Libraries | When to Use |
51-
|----------|-------------|-------------|------------------|-------------|
52-
| **[Complete rewrite](#complete-rewrite-to-aspnet-core-patterns)** | High - Rewrite all static principal access | Best | Requires updates | Complete rewrites, performance-critical apps |
53-
| **[System.Web adapters](#systemweb-adapters)** | Low - Keep existing patterns | Good | Works with existing code | Incremental migrations, extensive static access |
54-
55-
## Complete rewrite to ASP.NET Core patterns
56-
57-
Choose this approach when you're performing a complete migration or want the best performance and maintainability.
58-
59-
ASP.NET Core provides several options for retrieving the current authenticated user's <xref:System.Security.Claims.ClaimsPrincipal> without relying on static properties. This approach requires rewriting static principal access code but offers the most benefits in the long term.
60-
61-
### Complete rewrite pros and cons
62-
63-
| Pros | Cons |
64-
|------|------|
65-
| Best performance | Requires rewriting all static principal access code |
66-
| More testable (dependency injection) | No automatic migration path |
67-
| No static dependencies | Learning curve for new patterns |
68-
| Native ASP.NET Core implementation | Breaking change from Framework patterns |
69-
| Thread-safe by design | Potential refactoring across shared libraries |
70-
71-
### ASP.NET Core ClaimsPrincipal access patterns
72-
73-
There are several options for retrieving the current authenticated user's <xref:System.Security.Claims.ClaimsPrincipal> in ASP.NET Core in place of <xref:System.Security.Claims.ClaimsPrincipal.Current?displayProperty=nameWithType>:
74-
75-
* **<xref:Microsoft.AspNetCore.Mvc.ControllerBase.User?displayProperty=nameWithType>**
76-
* **<xref:Microsoft.AspNetCore.Http.HttpContext.User?displayProperty=nameWithType>**
77-
* **Passed in from caller**. Libraries without access to the current <xref:Microsoft.AspNetCore.Http.HttpContext> are often called from controllers or middleware components and can have the current user's identity passed as an argument.
78-
* **<xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>**. The project being migrated to ASP.NET Core may be too large to easily pass the current user's identity to all necessary locations. In such cases, <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> can be used as a workaround. This is not ideal as it uses a static accessor behind the scenes. Prefer a more direct option if possible.
79-
80-
### Code examples
81-
82-
Here are examples of migrating common static principal usage patterns:
83-
84-
**ASP.NET Framework (before):**
85-
```csharp
86-
public class UserService
87-
{
88-
public string GetCurrentUserId()
89-
{
90-
// Both ClaimsPrincipal.Current and Thread.CurrentPrincipal work interchangeably
91-
return ClaimsPrincipal.Current?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
92-
// or: return Thread.CurrentPrincipal?.Identity?.Name;
93-
}
94-
}
95-
```
96-
97-
**ASP.NET Core (after) - Pass ClaimsPrincipal as parameter:**
98-
```csharp
99-
public class UserService
100-
{
101-
public string GetCurrentUserId(ClaimsPrincipal user)
102-
{
103-
return user?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
104-
}
105-
}
106-
107-
// Usage in controller
108-
public class HomeController : Controller
109-
{
110-
private readonly UserService _userService;
111-
112-
public HomeController(UserService userService)
113-
{
114-
_userService = userService;
115-
}
116-
117-
public IActionResult Index()
118-
{
119-
var userId = _userService.GetCurrentUserId(User);
120-
return View();
121-
}
122-
}
123-
```
124-
125-
**ASP.NET Core (after) - Dependency Injection:**
126-
```csharp
127-
public class UserService
128-
{
129-
private readonly IHttpContextAccessor _httpContextAccessor;
130-
131-
public UserService(IHttpContextAccessor httpContextAccessor)
132-
{
133-
_httpContextAccessor = httpContextAccessor;
134-
}
135-
136-
public string GetCurrentUserId()
137-
{
138-
return _httpContextAccessor.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
139-
}
140-
}
141-
```
142-
143-
### When to choose this approach
144-
145-
* You can afford to rewrite static principal access code
146-
* Performance is a top priority (in this case, prefer passing the identity as a parameter over DI)
147-
* You want to eliminate static dependencies
148-
* You're not sharing code with legacy applications
149-
* You want the most testable and maintainable solution
150-
151-
## System.Web adapters
152-
153-
[!INCLUDE[](~/migration/fx-to-core/includes/uses-systemweb-adapters.md)]
154-
155-
Choose this approach when you need to maintain existing static principal usage patterns during incremental migration, or when you have extensive shared libraries that would be difficult to update.
156-
157-
The System.Web adapters can enable both <xref:System.Security.Claims.ClaimsPrincipal.Current?displayProperty=nameWithType> and <xref:System.Threading.Thread.CurrentPrincipal?displayProperty=nameWithType> support in ASP.NET Core, allowing you to keep existing code patterns while migrating incrementally. Both properties work interchangeably once adapters are configured.
158-
159-
### System.Web adapters pros and cons
160-
161-
| Pros | Cons |
162-
|------|------|
163-
| Minimal code changes required | Performance overhead |
164-
| Works with existing shared libraries | Not thread-safe in all scenarios |
165-
| Enables incremental migration | Requires System.Web adapters dependency |
166-
| Maintains familiar patterns | Should be temporary solution |
167-
| Good for large codebases | Less testable than DI patterns |
168-
169-
### Setting up static principal support
170-
171-
To enable static principal support (<xref:System.Security.Claims.ClaimsPrincipal.Current?displayProperty=nameWithType> and <xref:System.Threading.Thread.CurrentPrincipal?displayProperty=nameWithType>) with System.Web adapters, endpoints must be annotated with the `SetThreadCurrentPrincipalAttribute` metadata:
172-
173-
```csharp
174-
// Add to controller or action
175-
[SetThreadCurrentPrincipal]
176-
public class HomeController : Controller
177-
{
178-
public IActionResult Index()
179-
{
180-
// Both ClaimsPrincipal.Current and Thread.CurrentPrincipal are now available
181-
var user1 = ClaimsPrincipal.Current;
182-
var user2 = Thread.CurrentPrincipal;
183-
return View();
184-
}
185-
}
186-
```
187-
188-
### When to use System.Web adapters
189-
190-
* You have extensive static principal usage across shared libraries
191-
* You're doing an incremental migration
192-
* You can't afford to rewrite all static principal access code immediately
193-
* You need to maintain compatibility with existing ASP.NET Framework code
194-
* You understand the performance and threading implications
195-
196-
## Migration considerations
197-
198-
### Performance implications
199-
200-
* **Native ASP.NET Core patterns** provide the best performance with no overhead
201-
* **System.Web adapters** introduce some performance overhead but enable gradual migration
202-
* **Static variables** should be avoided as they can cause memory leaks and threading issues
31+
The final option, using an `IHttpContextAccessor` instance stored in a static variable, is contrary to the ASP.NET Core principle of preferring injected dependencies to static dependencies. Plan to eventually retrieve `IHttpContextAccessor` instances from DI instead. A static helper can be a useful bridge, though, when migrating large existing ASP.NET apps that use `ClaimsPrincipal.Current`.

0 commit comments

Comments
 (0)