Add WmsLite monorepo with JWT auth, tenant context, and project READMEs.

Implement API security baseline (sys_user, JWT, audit fields, transactions,
unique constraints) and Web real login with 401 handling. Document setup
and development workflows in root, WmsLite.Api, and WmsLite.Web README files.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zhougfandCursor committed 2026-05-24 14:17:47 +08:00
commit fa1b286463
241 files changed
+31188

No files matched your search

@@ -0,0 +1,56 @@
using System.Security.Claims;
namespace WmsLite.Api.Infrastructure.Auth;
public sealed class HttpCurrentUserContext : ICurrentUserContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HttpCurrentUserContext(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
private ClaimsPrincipal? User => _httpContextAccessor.HttpContext?.User;
public bool IsAuthenticated => User?.Identity?.IsAuthenticated == true;
public string UserId =>
FindClaim(AuthClaimTypes.UserId)
?? FindClaim(System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames.Sub)
?? RequireClaim(AuthClaimTypes.UserId);
public string TenantId => RequireClaim(AuthClaimTypes.TenantId);
public string OwnerCode => RequireClaim(AuthClaimTypes.OwnerCode);
public string? DisplayName => User?.FindFirst(AuthClaimTypes.DisplayName)?.Value
?? User?.FindFirst(ClaimTypes.Name)?.Value;
private string? FindClaim(string type) =>
User?.FindFirst(type)?.Value is { Length: > 0 } value ? value.Trim() : null;
private string RequireClaim(string type)
{
if (!IsAuthenticated)
{
throw new UnauthorizedAccessException("User is not authenticated.");
}
var value = User!.FindFirst(type)?.Value;
if (string.IsNullOrWhiteSpace(value))
{
throw new UnauthorizedAccessException($"Missing claim: {type}.");
}
return value.Trim();
}
}
public static class AuthClaimTypes
{
public const string UserId = "sub";
public const string TenantId = "tenant_id";
public const string OwnerCode = "owner_code";
public const string DisplayName = "name";
}
@@ -0,0 +1,14 @@
namespace WmsLite.Api.Infrastructure.Auth;
public interface ICurrentUserContext
{
bool IsAuthenticated { get; }
string UserId { get; }
string TenantId { get; }
string OwnerCode { get; }
string? DisplayName { get; }
}
@@ -0,0 +1,14 @@
namespace WmsLite.Api.Infrastructure.Auth;
public class JwtOptions
{
public const string SectionName = "Jwt";
public string Issuer { get; set; } = "WmsLite";
public string Audience { get; set; } = "WmsLite.Web";
public string Key { get; set; } = string.Empty;
public int ExpiresMinutes { get; set; } = 480;
}
@@ -0,0 +1,52 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using WmsLite.Api.Models;
namespace WmsLite.Api.Infrastructure.Auth;
public interface IJwtTokenService
{
(string Token, DateTime ExpiresAt) CreateToken(SysUser user);
}
public sealed class JwtTokenService : IJwtTokenService
{
private readonly JwtOptions _options;
public JwtTokenService(IOptions<JwtOptions> options)
{
_options = options.Value;
}
public (string Token, DateTime ExpiresAt) CreateToken(SysUser user)
{
if (string.IsNullOrWhiteSpace(_options.Key))
{
throw new InvalidOperationException("Jwt:Key is not configured.");
}
var expires = DateTime.UtcNow.AddMinutes(_options.ExpiresMinutes <= 0 ? 480 : _options.ExpiresMinutes);
var claims = new List<Claim>
{
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new(AuthClaimTypes.TenantId, user.TenantId),
new(AuthClaimTypes.OwnerCode, user.OwnerCode),
new(AuthClaimTypes.DisplayName, user.DisplayName ?? user.Username),
new(JwtRegisteredClaimNames.UniqueName, user.Username),
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_options.Key));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _options.Issuer,
audience: _options.Audience,
claims: claims,
expires: expires,
signingCredentials: creds);
return (new JwtSecurityTokenHandler().WriteToken(token), expires);
}
}