Skip to content

Commit a44dd16

Browse files
committedFeb 9, 2025
.net 8 security #203
1 parent 87e87b8 commit a44dd16

File tree

15 files changed

+76
-129
lines changed

15 files changed

+76
-129
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
32
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
54
<Nullable>enable</Nullable>
65
<ImplicitUsings>enable</ImplicitUsings>
76
</PropertyGroup>
8-
97
<ItemGroup>
10-
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.3" />
11-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0" />
8+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
9+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.7" />
1210
</ItemGroup>
13-
14-
</Project>
11+
</Project>

‎2_Libs/Security/ASPNETCoreMVCSecurity/Controllers/HomeController.cs

+2-11
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,8 @@
88

99
namespace ASPNETCoreMVCSecurity.Controllers;
1010

11-
public class HomeController : Controller
11+
public class HomeController(ILogger<HomeController> logger, IConfiguration configuration) : Controller
1212
{
13-
private readonly ILogger<HomeController> _logger;
14-
private readonly IConfiguration _settings;
15-
16-
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
17-
{
18-
_logger = logger;
19-
_settings = configuration;
20-
}
21-
2213
public IActionResult Index() => View();
2314

2415
public IActionResult Privacy() => View();
@@ -35,7 +26,7 @@ public IActionResult EchoWithView(string x)
3526

3627
public IActionResult SqlSample(string id)
3728
{
38-
string connectionString = _settings.GetConnectionString("InjectionConnection") ?? throw new InvalidOperationException("can't find InjectionConnection");
29+
string connectionString = configuration.GetConnectionString("InjectionConnection") ?? throw new InvalidOperationException("can't find InjectionConnection");
3930
SqlConnection sqlConnection = new(connectionString);
4031
SqlCommand command = sqlConnection.CreateCommand();
4132

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
namespace ASPNETCoreMVCSecurity.Models;
22

3-
public record Book(string Title, string? Publisher);
3+
public record class Book(string Title, string? Publisher);
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
32
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
54
<Nullable>enable</Nullable>
65
<ImplicitUsings>enable</ImplicitUsings>
76
</PropertyGroup>
8-
9-
</Project>
7+
</Project>

‎2_Libs/Security/IdentitySample/IdentitySample.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<UserSecretsId>1dd61b86-5b54-4a6a-bed2-fefbe07d2273</UserSecretsId>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
13-
<PackageReference Include="Microsoft.Identity.Client" Version="4.61.3" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
13+
<PackageReference Include="Microsoft.Identity.Client" Version="4.68.0" />
1414
</ItemGroup>
1515

1616
</Project>

‎2_Libs/Security/SecureTransfer/AliceRunner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
aes.GenerateIV();
2727
using ICryptoTransform encryptor = aes.CreateEncryptor();
2828
using MemoryStream ms = new();
29-
using (CryptoStream cs = new(ms, encryptor, CryptoStreamMode.Write))
29+
using (CryptoStream cs = new(ms, encryptor, CryptoStreamMode.Write, leaveOpen: true))
3030
{
3131
await cs.WriteAsync(plainData.AsMemory());
3232
} // need to close the CryptoStream before using the MemoryStream

‎2_Libs/Security/SecureTransfer/SecureTransfer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
1313
</ItemGroup>
1414

1515
</Project>

‎2_Libs/Security/SigningDemo/AliceRunner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public (byte[] Data, byte[] Sign) GetDocumentAndSignature()
1111
{
12-
_logger.LogInformation($"Using this ECDsa class: {_signAlgorithm.GetType().Name}");
12+
_logger.LogInformation("Using this ECDsa class: {Type}", _signAlgorithm.GetType().Name);
1313

1414
byte[] aliceData = Encoding.UTF8.GetBytes("I'm Alice");
1515
byte[] aliceDataSignature = _signAlgorithm.SignData(aliceData, HashAlgorithmName.SHA512);

‎2_Libs/Security/SigningDemo/SigningDemo.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
1212
</ItemGroup>
1313

1414
</Project>

‎2_Libs/Security/WebAppWithADSample/Pages/Index.cshtml.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace WebAppWithADSample.Pages;
44

5-
public class IndexModel : PageModel
5+
public class IndexModel(ILogger<IndexModel> logger) : PageModel
66
{
7-
private readonly ILogger<IndexModel> _logger;
8-
9-
public IndexModel(ILogger<IndexModel> logger) => _logger = logger;
10-
117
public void OnGet()
128
{
139

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
namespace WebAppWithADSample;
1+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
2+
using Microsoft.Identity.Web;
3+
using Microsoft.Identity.Web.UI;
24

3-
public class Program
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
8+
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
9+
10+
builder.Services.AddAuthorization(options =>
411
{
5-
public static void Main(string[] args)
12+
options.AddPolicy("Developers", policy =>
613
{
7-
CreateHostBuilder(args).Build().Run();
8-
}
9-
10-
public static IHostBuilder CreateHostBuilder(string[] args) =>
11-
Host.CreateDefaultBuilder(args)
12-
.ConfigureWebHostDefaults(webBuilder =>
13-
{
14-
webBuilder.UseStartup<Startup>();
15-
});
14+
policy.RequireRole("DevGroup");
15+
});
16+
options.AddPolicy("Employees", policy =>
17+
{
18+
policy.RequireClaim("EmployeeNumber");
19+
});
20+
// By default, all incoming requests will be authorized according to the default policy
21+
options.FallbackPolicy = options.DefaultPolicy;
22+
});
23+
24+
builder.Services.AddRazorPages()
25+
.AddMvcOptions(options => { })
26+
.AddMicrosoftIdentityUI();
27+
28+
var app = builder.Build();
29+
30+
if (app.Environment.IsDevelopment())
31+
{
32+
app.UseDeveloperExceptionPage();
1633
}
34+
else
35+
{
36+
app.UseExceptionHandler("/Error");
37+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
38+
app.UseHsts();
39+
}
40+
41+
app.UseHttpsRedirection();
42+
app.UseStaticFiles();
43+
44+
app.UseRouting();
45+
46+
app.UseAuthentication();
47+
app.UseAuthorization();
48+
49+
app.MapRazorPages();
50+
app.MapControllers();
51+
52+
app.Run();

‎2_Libs/Security/WebAppWithADSample/Startup.cs

-68
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
32
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
54
<UserSecretsId>aspnet-WebAppWithADSample-D3F1D045-6494-4AF9-ABF2-19D69C600B1D</UserSecretsId>
65
<Nullable>enable</Nullable>
76
<ImplicitUsings>enable</ImplicitUsings>
87
</PropertyGroup>
9-
108
<ItemGroup>
11-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.0" NoWarn="NU1605" />
12-
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.0" NoWarn="NU1605" />
13-
<PackageReference Include="Microsoft.Identity.Web" Version="1.25.5" />
14-
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.25.5" />
9+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.12" NoWarn="NU1605" />
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.12" NoWarn="NU1605" />
11+
<PackageReference Include="Microsoft.Identity.Web" Version="3.7.0" />
12+
<PackageReference Include="Microsoft.Identity.Web.UI" Version="3.7.0" />
1513
</ItemGroup>
16-
17-
</Project>
14+
</Project>

‎2_Libs/Security/X509CertificateSample/KeyVaultService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public KeyVaultService(IConfiguration configuration, ILogger<KeyVaultService> lo
3434
_vaultUri = configuration["VaultUri"] ?? throw new InvalidOperationException("VaultUri configuration missing");
3535
_logger = logger;
3636
_azureEventSourceListener = new AzureEventSourceListener((eventArgs, message)
37-
=> _logger.Log(eventArgs.Level.ToLogLevel(), message), EventLevel.Verbose);
37+
=> _logger.Log(eventArgs.Level.ToLogLevel(), "{Message}", message), EventLevel.Verbose);
3838
}
3939

4040
public void Dispose()

‎2_Libs/Security/X509CertificateSample/X509CertificateSample.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.Identity" Version="1.11.4" />
12-
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.5.1" />
13-
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
14-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
11+
<PackageReference Include="Azure.Identity" Version="1.13.2" />
12+
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.7.0" />
13+
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.7.0" />
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

0 commit comments

Comments
 (0)
Please sign in to comment.