gpt4 book ai didi

jwt - 发布项目后出现异常 "System.Security.Cryptography.CryptographicException"

转载 作者:行者123 更新时间:2023-12-05 05:44:57 31 4
gpt4 key购买 nike

每次我将我的 Blazor 服务器项目发布到我的网站域并打开该网站时,都会发生此异常,谷歌搜索几乎没有帮助:

enter image description here

它说 AppState.cs: line 21,所以这是它的代码行:

enter image description here

调试本地主机时不会发生此异常。当我从我网站上的浏览器中删除 localStorage 并刷新时,一切正常。但我不希望我的客户遇到此异常,并且在我每次发布时都必须告诉他们删除本地存储。

如果需要,我的 Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor().AddCircuitOptions(options => options.DetailedErrors = true);
builder.Services.AddHttpClient();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters.ValidateIssuerSigningKey = true; // Validér secret key for JWT
options.TokenValidationParameters.ValidateLifetime = false; // Validér ikke Lifetime på JWT
options.TokenValidationParameters.ValidateAudience = false; // Ikke validér clients(audience), fx BlazorWeb, der skal anvende IdentityServer
options.TokenValidationParameters.ValidateIssuer = false; // Ikke validér IdentityServer(issuer)
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:Secret"])); // Sæt secret key for JWT, der bruges som adgangskode til at tilgå JWT
});

builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<AuthenticationStateProvider, AppState>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

RewriteOptions options = new RewriteOptions();
options.AddRedirectToWww();
options.AddRedirectToHttps();
app.UseRewriter(options);

app.UseStaticFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

最佳答案

经过数小时的研究,我设法修复了它。我做了以下事情;添加了 builder.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"c:\your\path\to\store\keys"));

关于jwt - 发布项目后出现异常 "System.Security.Cryptography.CryptographicException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71494715/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com