我正在尝试使用Jason Taylor 的简洁架构模板,这个模板使用 NSwag 自动创建 TypeScript 客户端(Angular),但我不需要创建 TS 客户端,所以我的主要目标是用 Razor Pages 替换它。我已经能够取得高分,但我在这方面遇到了麻烦当前用户服务每当实例化它时,都应该在此行中设置 UserId:
UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
这是完整的当前用户服务:
using CleanREC0.Application.Common.Interfaces;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
namespace CleanREC0.WebUI.Services
{
public class CurrentUserService : ICurrentUserService
{
public CurrentUserService(IHttpContextAccessor httpContextAccessor)
{
UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
}
public string UserId { get; }
}
}
第一次甚至 HttpContext 是null这很好,因为一切都在初始化,但后续调用总是返回null在所有的httpContextAccessor.HttpContext.User.Identity属性,用户显示Identity好吧,但它的所有属性和声明都是null or 没有结果,即使用户正确登录。
以下是我对模板所做的操作的列表:
- 摆脱NSwag和所有打字稿 Related
- 摆脱身份服务器
- 已更换ApiAuthorizationDbContext for 身份数据库上下文
这是我的启动课程:
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddApplication();
services.AddInfrastructure(Configuration, Environment);
services.AddScoped<ICurrentUserService, CurrentUserService>();
services.AddHttpContextAccessor();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); //I have added this line on as suggested on other post, but made no difference.
services.AddHealthChecks()
.AddDbContextCheck<ApplicationDbContext>();
services.AddRazorPages()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<IApplicationDbContext>());
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseCustomExceptionHandler();
app.UseHealthChecks("/health");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
这是添加应用程序被调用的配置服务:
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPerformanceBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
return services;
}
And the 添加基础设施那里也被调用:
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
if (environment.IsEnvironment("Test"))
{
}
else
{
services.AddTransient<IDateTime, DateTimeService>();
services.AddTransient<IIdentityService, IdentityService>();
services.AddTransient<ICsvFileBuilder, CsvFileBuilder>();
}
services.AddAuthentication();
return services;
}
其他一切似乎都运行良好。在剃刀页面水平User变量,它是Identity包含所有预期的信息和声明。
我确信我错过了一些东西,有人可以指出我正确的方向吗?