如何解决Blazor服务器中的身份验证令牌
我有一个ASP.net核心WEB API,我已经使用本地用户帐户(使用WASM blazor模板创建它)保护了它的安全。我可以使用WASM客户端成功进行身份验证并调用webapi。
我有一个单独的服务器端Blazor应用程序,尝试调用此WebAPI,该WebAPI再次使用本地用户帐户和相同的身份数据库进行了保护。
在wasm(webapi)应用中,我按如下方式配置安全性:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,builder =>
{
builder.WithOrigins("https://localhost:44339");
});
});
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser,ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
services.AddRazorPages();
在服务器端应用程序中,我按如下方式配置安全性:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => { options.SignIn.RequireConfirmedAccount = true; })
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider,RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddHttpClient();
然后在服务器端应用程序中,我尝试在_Host.cshtml文件中获取令牌,以便可以将其添加到客户端标头中,但它们始终返回为空。
AccessToken = await HttpContext.GetTokenAsync("access_token"),RefreshToken = await HttpContext.GetTokenAsync("refresh_token")
在安全性方面,我是一位新手,非常感谢,我可能完全错了。任何人都可以给我一个指示,指出我是在做错事,还是需要做其他事情来获取这些令牌?
非常感谢 戈登
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。