Skip to content

Commit

Permalink
Merge pull request #35 from netcorepal/v23
Browse files Browse the repository at this point in the history
升级2.3.0,添加命令锁、JwtProvider示例
  • Loading branch information
witskeeper authored Feb 20, 2025
2 parents a00d012 + 1ef4f9b commit d8b591a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
2 changes: 1 addition & 1 deletion eng/versions.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>2.2.1</VersionPrefix>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion template/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<NetCorePalVersion>2.2.0</NetCorePalVersion>
<NetCorePalVersion>2.3.0</NetCorePalVersion>
<FrameworkVersion>9.0.0</FrameworkVersion>
<ExtensionsVersion>9.0.0</ExtensionsVersion>
<EntityFrameworkVersion>9.0.0</EntityFrameworkVersion>
Expand Down
1 change: 1 addition & 0 deletions template/src/ABC.Template.Web/ABC.Template.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<PackageReference Include="NetCorePal.Extensions.DistributedTransactions.CAP.MySql" Version="$(NetCorePalVersion)" />
<PackageReference Include="NetCorePal.Extensions.MultiEnv" Version="$(NetCorePalVersion)" />
<PackageReference Include="NetCorePal.Extensions.Primitives" Version="$(NetCorePalVersion)" />
<PackageReference Include="NetCorePal.Extensions.Jwt.StackExchangeRedis" Version="$(NetCorePalVersion)" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="9.0.0-preview.2.efcore.9.0.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" Version="8.2.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ namespace ABC.Template.Web.Application.Commands;

public record class OrderPaidCommand(OrderId OrderId) : ICommand;

public class OrderPaidCommandLock : ICommandLock<OrderPaidCommand>
{
public Task<CommandLockSettings> GetLockKeysAsync(OrderPaidCommand command,
CancellationToken cancellationToken = new CancellationToken())
{
return Task.FromResult(command.OrderId.ToCommandLockSettings());
}
}

public class OrderPaidCommandHandler(IOrderRepository orderRepository) : ICommandHandler<OrderPaidCommand>
{
public async Task Handle(OrderPaidCommand request, CancellationToken cancellationToken)
Expand Down
2 changes: 0 additions & 2 deletions template/src/ABC.Template.Web/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using DotNetCore.CAP;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using NetCorePal.Extensions.DistributedTransactions.Sagas;
using NetCorePal.Extensions.Domain;
using NetCorePal.Extensions.Dto;

namespace ABC.Template.Web.Controllers;
Expand Down
31 changes: 31 additions & 0 deletions template/src/ABC.Template.Web/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCorePal.Extensions.Dto;
using NetCorePal.Extensions.Jwt;

namespace ABC.Template.Web.Controllers;

[Route("user")]
public class UserController
{
[Route("login")]
[HttpPost]
public async Task<ResponseData<string>> Login(string username, string password,
[FromServices] IJwtProvider jwtProvider)
{
var jwt = await jwtProvider.GenerateJwtToken(
new JwtData("netcorepal", "netcorepal",
[new Claim("name", username)],
DateTime.Now, DateTime.Now.AddDays(1)));
return jwt.AsResponseData();
}

[Route("auth")]
[HttpGet]
[Authorize(AuthenticationSchemes = "Bearer")]
public Task<ResponseData<bool>> Auth()
{
return Task.FromResult(true.AsResponseData());
}
}
19 changes: 15 additions & 4 deletions template/src/ABC.Template.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
builder.Services.AddDataProtection()
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");

builder.Services.AddAuthentication().AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.TokenValidationParameters.ValidAudience = "netcorepal";
options.TokenValidationParameters.ValidateAudience = true;
options.TokenValidationParameters.ValidIssuer = "netcorepal";
options.TokenValidationParameters.ValidateIssuer = true;
});
builder.Services.AddNetCorePalJwt().AddRedisStore();

#endregion

#region Controller
Expand Down Expand Up @@ -96,8 +106,8 @@
#endregion



#region 基础设施

builder.Services.AddRepositories(typeof(ApplicationDbContext).Assembly);

builder.Services.AddDbContext<ApplicationDbContext>(options =>
Expand Down Expand Up @@ -133,6 +143,7 @@

builder.Services.AddMediatR(cfg =>
cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly())
.AddCommandLockBehavior()
.AddKnownExceptionValidationBehavior()
.AddUnitOfWorkBehaviors());

Expand Down Expand Up @@ -164,7 +175,7 @@
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.EnsureCreated();
await dbContext.Database.EnsureCreatedAsync();
}

app.UseKnownExceptionHandler();
Expand Down Expand Up @@ -192,15 +203,15 @@
app.MapHealthChecks("/health");
app.MapMetrics("/metrics"); // 通过 /metrics 访问指标
app.UseHangfireDashboard();
app.Run();
await app.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
await Log.CloseAndFlushAsync();
}

#pragma warning disable S1118
Expand Down
21 changes: 18 additions & 3 deletions template/test/ABC.Template.Web.Tests/DemoTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Net.Http.Headers;
using ABC.Template.Infrastructure;
using ABC.Template.Web.Controllers;
using ABC.Template.Web.Tests.Extensions;
using Microsoft.EntityFrameworkCore;
using Moq;
using NetCorePal.Context;
using NetCorePal.Extensions.AspNetCore;
using NetCorePal.Extensions.Dto;

namespace ABC.Template.Web.Tests
Expand Down Expand Up @@ -134,5 +132,22 @@ public async Task LockTest()
Assert.False(result1.Data);
Assert.False(result2.Data);
}


[Fact]
public async Task JwtTest()
{
string userName = "testname";
string password = "testpassword";
var response = await _client.PostAsync($"/user/login?username={userName}&password={password}", null);
Assert.True(response.IsSuccessStatusCode);
var responseData = await response.Content.ReadFromNewtonsoftJsonAsync<ResponseData<string>>();
Assert.NotNull(responseData);
Assert.NotNull(responseData.Data);

_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", responseData.Data);
var jwtResponse = await _client.GetAsync("/user/auth");
Assert.True(jwtResponse.IsSuccessStatusCode);
}
}
}

0 comments on commit d8b591a

Please sign in to comment.