Skip to content

Commit 9d28720

Browse files
committed
Update templates to use file-scoped namespace declarations
Contributes to #33947
1 parent d8bba72 commit 9d28720

File tree

24 files changed

+497
-519
lines changed

24 files changed

+497
-519
lines changed

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorServerWeb-CSharp/Areas/Identity/RevalidatingIdentityAuthenticationStateProvider.cs

+46-47
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,65 @@
55
using Microsoft.AspNetCore.Identity;
66
using Microsoft.Extensions.Options;
77

8-
namespace BlazorServerWeb_CSharp.Areas.Identity
8+
namespace BlazorServerWeb_CSharp.Areas.Identity;
9+
10+
public class RevalidatingIdentityAuthenticationStateProvider<TUser>
11+
: RevalidatingServerAuthenticationStateProvider where TUser : class
912
{
10-
public class RevalidatingIdentityAuthenticationStateProvider<TUser>
11-
: RevalidatingServerAuthenticationStateProvider where TUser : class
13+
private readonly IServiceScopeFactory _scopeFactory;
14+
private readonly IdentityOptions _options;
15+
16+
public RevalidatingIdentityAuthenticationStateProvider(
17+
ILoggerFactory loggerFactory,
18+
IServiceScopeFactory scopeFactory,
19+
IOptions<IdentityOptions> optionsAccessor)
20+
: base(loggerFactory)
1221
{
13-
private readonly IServiceScopeFactory _scopeFactory;
14-
private readonly IdentityOptions _options;
22+
_scopeFactory = scopeFactory;
23+
_options = optionsAccessor.Value;
24+
}
1525

16-
public RevalidatingIdentityAuthenticationStateProvider(
17-
ILoggerFactory loggerFactory,
18-
IServiceScopeFactory scopeFactory,
19-
IOptions<IdentityOptions> optionsAccessor)
20-
: base(loggerFactory)
26+
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
27+
28+
protected override async Task<bool> ValidateAuthenticationStateAsync(
29+
AuthenticationState authenticationState, CancellationToken cancellationToken)
30+
{
31+
// Get the user manager from a new scope to ensure it fetches fresh data
32+
var scope = _scopeFactory.CreateScope();
33+
try
2134
{
22-
_scopeFactory = scopeFactory;
23-
_options = optionsAccessor.Value;
35+
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>();
36+
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
2437
}
25-
26-
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
27-
28-
protected override async Task<bool> ValidateAuthenticationStateAsync(
29-
AuthenticationState authenticationState, CancellationToken cancellationToken)
38+
finally
3039
{
31-
// Get the user manager from a new scope to ensure it fetches fresh data
32-
var scope = _scopeFactory.CreateScope();
33-
try
40+
if (scope is IAsyncDisposable asyncDisposable)
3441
{
35-
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>();
36-
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
42+
await asyncDisposable.DisposeAsync();
3743
}
38-
finally
44+
else
3945
{
40-
if (scope is IAsyncDisposable asyncDisposable)
41-
{
42-
await asyncDisposable.DisposeAsync();
43-
}
44-
else
45-
{
46-
scope.Dispose();
47-
}
46+
scope.Dispose();
4847
}
4948
}
49+
}
5050

51-
private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal)
51+
private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal)
52+
{
53+
var user = await userManager.GetUserAsync(principal);
54+
if (user == null)
5255
{
53-
var user = await userManager.GetUserAsync(principal);
54-
if (user == null)
55-
{
56-
return false;
57-
}
58-
else if (!userManager.SupportsUserSecurityStamp)
59-
{
60-
return true;
61-
}
62-
else
63-
{
64-
var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType);
65-
var userStamp = await userManager.GetSecurityStampAsync(user);
66-
return principalStamp == userStamp;
67-
}
56+
return false;
57+
}
58+
else if (!userManager.SupportsUserSecurityStamp)
59+
{
60+
return true;
61+
}
62+
else
63+
{
64+
var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType);
65+
var userStamp = await userManager.GetSecurityStampAsync(user);
66+
return principalStamp == userStamp;
6867
}
6968
}
7069
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore;
33

4-
namespace BlazorServerWeb_CSharp.Data
4+
namespace BlazorServerWeb_CSharp.Data;
5+
6+
public class ApplicationDbContext : IdentityDbContext
57
{
6-
public class ApplicationDbContext : IdentityDbContext
8+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
9+
: base(options)
710
{
8-
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
9-
: base(options)
10-
{
11-
}
1211
}
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
namespace BlazorServerWeb_CSharp.Data
1+
namespace BlazorServerWeb_CSharp.Data;
2+
3+
public class WeatherForecast
24
{
3-
public class WeatherForecast
4-
{
5-
public DateTime Date { get; set; }
5+
public DateTime Date { get; set; }
66

7-
public int TemperatureC { get; set; }
7+
public int TemperatureC { get; set; }
88

9-
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1010

11-
public string? Summary { get; set; }
12-
}
11+
public string? Summary { get; set; }
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
namespace BlazorServerWeb_CSharp.Data
1+
namespace BlazorServerWeb_CSharp.Data;
2+
3+
public class WeatherForecastService
24
{
3-
public class WeatherForecastService
5+
private static readonly string[] Summaries = new[]
46
{
5-
private static readonly string[] Summaries = new[]
6-
{
7-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
8-
};
7+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
8+
};
99

10-
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
10+
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
11+
{
12+
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
1113
{
12-
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
13-
{
14-
Date = startDate.AddDays(index),
15-
TemperatureC = Random.Shared.Next(-20, 55),
16-
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
17-
}).ToArray());
18-
}
14+
Date = startDate.AddDays(index),
15+
TemperatureC = Random.Shared.Next(-20, 55),
16+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
17+
}).ToArray());
1918
}
2019
}

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorServerWeb-CSharp/Pages/Error.cshtml.cs

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.AspNetCore.Mvc.RazorPages;
44

5-
namespace BlazorServerWeb_CSharp.Pages
5+
namespace BlazorServerWeb_CSharp.Pages;
6+
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
610
{
7-
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8-
[IgnoreAntiforgeryToken]
9-
public class ErrorModel : PageModel
10-
{
11-
public string? RequestId { get; set; }
11+
public string? RequestId { get; set; }
1212

13-
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
1414

15-
private readonly ILogger<ErrorModel> _logger;
15+
private readonly ILogger<ErrorModel> _logger;
1616

17-
public ErrorModel(ILogger<ErrorModel> logger)
18-
{
19-
_logger = logger;
20-
}
17+
public ErrorModel(ILogger<ErrorModel> logger)
18+
{
19+
_logger = logger;
20+
}
2121

22-
public void OnGet()
23-
{
24-
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25-
}
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
2625
}
2726
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
22
using Microsoft.AspNetCore.Mvc;
33

4-
namespace ComponentsWebAssembly_CSharp.Server.Controllers
4+
namespace ComponentsWebAssembly_CSharp.Server.Controllers;
5+
6+
public class OidcConfigurationController : Controller
57
{
6-
public class OidcConfigurationController : Controller
7-
{
8-
private readonly ILogger<OidcConfigurationController> _logger;
8+
private readonly ILogger<OidcConfigurationController> _logger;
99

10-
public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider, ILogger<OidcConfigurationController> logger)
11-
{
12-
ClientRequestParametersProvider = clientRequestParametersProvider;
13-
_logger = logger;
14-
}
10+
public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider, ILogger<OidcConfigurationController> logger)
11+
{
12+
ClientRequestParametersProvider = clientRequestParametersProvider;
13+
_logger = logger;
14+
}
1515

16-
public IClientRequestParametersProvider ClientRequestParametersProvider { get; }
16+
public IClientRequestParametersProvider ClientRequestParametersProvider { get; }
1717

18-
[HttpGet("_configuration/{clientId}")]
19-
public IActionResult GetClientRequestParameters([FromRoute]string clientId)
20-
{
21-
var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
22-
return Ok(parameters);
23-
}
18+
[HttpGet("_configuration/{clientId}")]
19+
public IActionResult GetClientRequestParameters([FromRoute]string clientId)
20+
{
21+
var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
22+
return Ok(parameters);
2423
}
2524
}

0 commit comments

Comments
 (0)