kar*_*rim 6 asp.net-core-identity identityserver5 .net-6.0
我正在开发一个使用 ASP.NET Core 3.1 Web API 构建的后端项目。一切都工作正常,直到我决定将我的项目迁移到 .NET 6,我已根据新的目标框架更新了所有库。
现在,当运行我的 Web API 时,检查运行状况 Url 工作正常,但是当尝试打开 swagger 或调用我的 Web API 的端点时,我收到此错误:
“IdentityServer:Key”配置部分未配置任何签名凭据。
当我使用 .NET Core 3.1 时,我没有遇到这个问题,我做了一些谷歌搜索,我发现添加这个函数.AddDeveloperSigningCredential()可以解决这个问题,但这对我来说不起作用。
这是我的代码:
public static class ProgramExtensions
{
readonly static string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
private const string SecretKey = "**********************";
private static readonly SymmetricSecurityKey _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SecretKey));
public static IConfiguration? Configuration { get; set; }
public static WebApplicationBuilder ConfigureBuilder(this WebApplicationBuilder builder)
{
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json",
optional: true, reloadOnChange: true).Build();
AppDbContext.ConnectionString = Configuration.GetConnectionString("myCX");
builder.Services.AddSingleton<IEmailConfiguration>(Configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>());
builder.Services.AddApplication();
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
connectionString,
x => x.UseNetTopologySuite()
.CommandTimeout(10000)));
services.AddScoped<IAppDbContext>(provider => provider.GetService<AppDbContext>());
services.AddTransient(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
services.AddIdentity<ApplicationUser, IdentityRole>(opt =>
{
opt.Password.RequiredLength = 7;
opt.Password.RequireDigit = false;
opt.Password.RequireUppercase = false;
opt.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddApiAuthorization<ApplicationUser, AppDbContext>();
builder.Services.AddSingleton<IJwtFactory, JwtFactory>();
builder.Services.AddScoped<JwtIssuerOptions>();
// Get options from app settings
var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));
// Configure JwtIssuerOptions
builder.Services.Configure<JwtIssuerOptions>(options =>
{
options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
});
var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],
ValidateAudience = true,
ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],
ValidateIssuerSigningKey = true,
IssuerSigningKey = _signingKey,
RequireExpirationTime = false,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(configureOptions =>
{
configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
configureOptions.TokenValidationParameters = tokenValidationParameters;
configureOptions.SaveToken = true;
});
builder.Services.AddAuthorization();
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "COVAL API", Version = "v1" }));
builder.Services.AddHealthChecks()
.AddDbContextCheck<AppDbContext>();
return builder;
}
public static WebApplication ConfigureApplication(this WebApplication app)
{
app.UseCors(MyAllowSpecificOrigins);
app.UseCustomExceptionHandler();
app.UseHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json";
var response = new HealthCheckReponse
{
Status = report.Status.ToString(),
HealthChecks = report.Entries.Select(x => new IndividualHealthCheckResponse
{
Component = x.Key,
Status = x.Value.Status.ToString(),
Description = $"{JsonConvert.SerializeObject(x)}\n{x.Value.Data}\n{x.Value.Description}\n{x.Value.Exception?.Message}\n{x.Value.Exception?.StackTrace}"
}),
HealthCheckDuration = report.TotalDuration
};
await context.Response.WriteAsync(JsonConvert.SerializeObject(response));
}
});
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseIdentityServer();
app.UseRouting();
app.UseAuthorization();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
return app;
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我的代码中缺少什么或者我应该做什么来解决这个问题?
非常感谢。
| 归档时间: |
|
| 查看次数: |
2905 次 |
| 最近记录: |