Skip to content

Commit

Permalink
Update templates to use file scoped namespace declarations (#34220)
Browse files Browse the repository at this point in the history
* Update templates to use file-scoped namespace declarations
* Move to SDK version that supports file-scoped namespaces
* Make CA1305 a suggestion rather than error
  - New compiler now makes this appear when using a StringBuilder with string interpolation. Logged #34361 to follow up.

Contributes to #33947
  • Loading branch information
DamianEdwards authored Jul 15, 2021
1 parent 81e3824 commit 25767de
Show file tree
Hide file tree
Showing 27 changed files with 506 additions and 529 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dotnet_diagnostic.CA1018.severity = warning
dotnet_diagnostic.CA1047.severity = warning

# CA1305: Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = error
dotnet_diagnostic.CA1305.severity = suggestion

# CA1507: Use nameof to express symbol names
dotnet_diagnostic.CA1507.severity = warning
Expand Down
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"sdk": {
"version": "6.0.100-preview.7.21360.1"
"version": "6.0.100-preview.7.21364.4"
},
"tools": {
"dotnet": "6.0.100-preview.7.21360.1",
"dotnet": "6.0.100-preview.7.21364.4",
"runtimes": {
"dotnet/x64": [
"2.1.27",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace MyApp.Namespace
{
namespace MyApp.Namespace;

#if NameIsPage
public class IndexModel : Microsoft.AspNetCore.Mvc.RazorPages.PageModel
public class IndexModel : Microsoft.AspNetCore.Mvc.RazorPages.PageModel
#else
public class IndexModel : PageModel
public class IndexModel : PageModel
#endif
{
public void OnGet()
{
public void OnGet()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,65 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;

namespace BlazorServerWeb_CSharp.Areas.Identity
namespace BlazorServerWeb_CSharp.Areas.Identity;

public class RevalidatingIdentityAuthenticationStateProvider<TUser>
: RevalidatingServerAuthenticationStateProvider where TUser : class
{
public class RevalidatingIdentityAuthenticationStateProvider<TUser>
: RevalidatingServerAuthenticationStateProvider where TUser : class
private readonly IServiceScopeFactory _scopeFactory;
private readonly IdentityOptions _options;

public RevalidatingIdentityAuthenticationStateProvider(
ILoggerFactory loggerFactory,
IServiceScopeFactory scopeFactory,
IOptions<IdentityOptions> optionsAccessor)
: base(loggerFactory)
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly IdentityOptions _options;
_scopeFactory = scopeFactory;
_options = optionsAccessor.Value;
}

public RevalidatingIdentityAuthenticationStateProvider(
ILoggerFactory loggerFactory,
IServiceScopeFactory scopeFactory,
IOptions<IdentityOptions> optionsAccessor)
: base(loggerFactory)
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);

protected override async Task<bool> ValidateAuthenticationStateAsync(
AuthenticationState authenticationState, CancellationToken cancellationToken)
{
// Get the user manager from a new scope to ensure it fetches fresh data
var scope = _scopeFactory.CreateScope();
try
{
_scopeFactory = scopeFactory;
_options = optionsAccessor.Value;
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>();
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
}

protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);

protected override async Task<bool> ValidateAuthenticationStateAsync(
AuthenticationState authenticationState, CancellationToken cancellationToken)
finally
{
// Get the user manager from a new scope to ensure it fetches fresh data
var scope = _scopeFactory.CreateScope();
try
if (scope is IAsyncDisposable asyncDisposable)
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>();
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
await asyncDisposable.DisposeAsync();
}
finally
else
{
if (scope is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else
{
scope.Dispose();
}
scope.Dispose();
}
}
}

private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal)
private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal)
{
var user = await userManager.GetUserAsync(principal);
if (user == null)
{
var user = await userManager.GetUserAsync(principal);
if (user == null)
{
return false;
}
else if (!userManager.SupportsUserSecurityStamp)
{
return true;
}
else
{
var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType);
var userStamp = await userManager.GetSecurityStampAsync(user);
return principalStamp == userStamp;
}
return false;
}
else if (!userManager.SupportsUserSecurityStamp)
{
return true;
}
else
{
var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType);
var userStamp = await userManager.GetSecurityStampAsync(user);
return principalStamp == userStamp;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace BlazorServerWeb_CSharp.Data
namespace BlazorServerWeb_CSharp.Data;

public class ApplicationDbContext : IdentityDbContext
{
public class ApplicationDbContext : IdentityDbContext
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace BlazorServerWeb_CSharp.Data
namespace BlazorServerWeb_CSharp.Data;

public class WeatherForecast
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public DateTime Date { get; set; }

public int TemperatureC { get; set; }
public int TemperatureC { get; set; }

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

public string? Summary { get; set; }
}
public string? Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
namespace BlazorServerWeb_CSharp.Data
namespace BlazorServerWeb_CSharp.Data;

public class WeatherForecastService
{
public class WeatherForecastService
private static readonly string[] Summaries = new[]
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace BlazorServerWeb_CSharp.Pages
namespace BlazorServerWeb_CSharp.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;
private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}
public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
using Microsoft.AspNetCore.Mvc;

namespace ComponentsWebAssembly_CSharp.Server.Controllers
namespace ComponentsWebAssembly_CSharp.Server.Controllers;

public class OidcConfigurationController : Controller
{
public class OidcConfigurationController : Controller
{
private readonly ILogger<OidcConfigurationController> _logger;
private readonly ILogger<OidcConfigurationController> _logger;

public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider, ILogger<OidcConfigurationController> logger)
{
ClientRequestParametersProvider = clientRequestParametersProvider;
_logger = logger;
}
public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider, ILogger<OidcConfigurationController> logger)
{
ClientRequestParametersProvider = clientRequestParametersProvider;
_logger = logger;
}

public IClientRequestParametersProvider ClientRequestParametersProvider { get; }
public IClientRequestParametersProvider ClientRequestParametersProvider { get; }

[HttpGet("_configuration/{clientId}")]
public IActionResult GetClientRequestParameters([FromRoute]string clientId)
{
var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
return Ok(parameters);
}
[HttpGet("_configuration/{clientId}")]
public IActionResult GetClientRequestParameters([FromRoute]string clientId)
{
var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
return Ok(parameters);
}
}
Loading

0 comments on commit 25767de

Please sign in to comment.