Skip to content

Commit c8df02a

Browse files
committed
Updated code snippet
1 parent a088e92 commit c8df02a

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

05_security/aspnet_user_integration/AspNetIdentityUserRepository.cs renamed to 05_security/aspnet_user_integration/AspNetIdentityAuthenticationService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
/// Based on the example code provided by Microsoft
66
/// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-9.0&preserve-view=true
77
/// </summary>
8-
public class AspNetIdentityUserRepository : IUserRepository
8+
public class AspNetIdentityAuthenticationService : IAuthenticationService
99
{
1010
private readonly IHttpContextAccessor _httpContextAccessor;
1111

12-
public AspNetIdentityUserRepository(IHttpContextAccessor httpContextAccessor)
12+
public AspNetIdentityAuthenticationService(IHttpContextAccessor httpContextAccessor)
1313
{
1414
_httpContextAccessor = httpContextAccessor;
1515
}
1616

1717
/// <inheritdoc />
18-
public string? GetCurrentUserId()
18+
public string? GetCurrentAuthenticatedUserId()
1919
{
2020
// Returns the aspnet_User.Id of the authenticated user
2121
return _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
2222
}
2323
}
24-
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public interface IAuthenticationService
2+
{
3+
/// <summary>
4+
/// Returns the user name of the authenticated user
5+
/// </summary>
6+
/// <returns></returns>
7+
string? GetCurrentAuthenticatedUserId();
8+
}

05_security/aspnet_user_integration/IUserRepository.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Adding the HTTP Context accessor to be injected. This is needed by the AspNetIdentityUserRepository
22
// to resolve the current user.
33
builder.Services.AddHttpContextAccessor();
4-
builder.Services.AddTransient<IUserRepository, AspNetIdentityUserRepository>();
4+
builder.Services.AddTransient<IAuthenticationService, AspNetIdentityAuthenticationService>();

05_security/aspnet_user_integration/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ When reading or adding entities, you may need to determine which user is current
44

55
## Components
66

7-
### `AspNetIdentityUserRepository : IUserRepository`
7+
### `AspNetIdentityAuthenticationService : IAuthenticationService`
88
This class contains the logic to retrieve the currently authenticated user from the ASP.NET Identity Framework Middleware.
9-
- It implements `IUserRepository`, allowing you to mock authentication logic in unit tests.
9+
- It implements `IAuthenticationService`, allowing you to mock this authentication logic in unit tests.
1010

1111
### `Program.cs`
1212
Add the provided snippet to `Program.cs` to configure your dependency injection container. Not doing this will result in error at startup.
1313

1414
## Implementation Steps
1515

16-
1. Inject `IUserRepository` into the `ApiController` where you need access to the current user.
17-
2. Call `GetCurrentUserId()` to retrieve the currently authenticated user ID.
16+
1. Inject `IAuthenticationService` into the `ApiController` where you need access to the current user.
17+
2. Call `GetCurrentAuthenticatedUserId()` to retrieve the currently authenticated user ID.
1818
3. Use the retrieved user ID to filter or extend your queries.

0 commit comments

Comments
 (0)