我在 .NET5 中构建了我的第一个 Blazor WASM(客户端和服务器)应用程序,但遇到了身份验证问题。 我的目标是不使用 Identity,因为我不想使用 localstorage 并且只有 JWT(或类似的)cookie 身份验证,如下图:Chrome DevTools。
我已经尝试使用
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie( ... )
在服务器端,但我收到一条错误消息
There is no registered service of type 'Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider'.
。
我没有错误
builder.Services.AddOidcAuthentication
在客户端的 Program.cs 中,但应用程序在我登录后没有授权我(我的 cookie 中有一个令牌)。
我觉得我已经尝试了所有 Microsoft Docs 和教程,但都没有运气,所以如果有人有解决方案,我将不胜感激!
答案 0 :(得分:0)
我有项目 Blazor webassembly,它通过身份服务器进行身份验证 你可以在这里找到完整的代码 https://github.com/kdehia/BlazorSecureWithIdentity_API_GRPC
在 main.cs 中 你应该从 appsettings.json 获取配置值
builder.Services.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("oidc", options.ProviderOptions);
});
你的 appsettings.json 应该看起来像这样
{
"oidc": {
"Authority": "https://localhost:5005/",
"ClientId": "blazorWASM",
"DefaultScopes": [
"openid",
"profile",
"companyApi",
"ApplicationRole",
"GRPC"
],
"PostLogoutRedirectUri": "authentication/logout-callback",
"RedirectUri": "authentication/login-callback",
"ResponseType": "code"
}
}
下载完整的项目,如果您有任何问题,请告诉我