我有一个C#解决方案,其中包括:
我使用Identity Servers资源所有者密码流通过React应用程序登录。这有效,我又获得了访问令牌。
然后我在Web api上调用传递访问令牌的方法:
[Authorize(AuthenticationSchemes = "Bearer")]
public async Task<VMFranchiseGeoJson> GetByAuthorizedUser()
{
try
{
var token = Context.Request.Headers["Authorization"];
var user = (Context.User.Identity as ClaimsIdentity);
...
在登录后的第一个调用上运行良好,令牌包含Bearer令牌,而Context.User.Identity包含声明。但是,如果我重新加载/刷新进行此调用的页面,则该页面会第一次甚至几次运行(似乎不是在设定的时间间隔之后,并且很快会丢失标题2-3分钟),然后停止工作即授权标头不再存在。
但是...
它在对API的调用中传递,例如“授权:Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjAzMDg5OTk1YmJkNWVlY2 ...”
我在这里使用的是CORS,因为它是本地开发的,但我将其配置为:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
您是否必须以某种方式保留它们?
尝试了多种解决方案,更改了CORS策略,重写了Identity Server部分,更改了流程等
编辑身份验证配置@Alpha:
services.AddMvcCore().AddRazorViewEngine().AddAuthorization();
//services.AddAuthentication("Bearer")
// .AddIdentityServerAuthentication(options =>
// {
// options.Authority = "http://localhost:5000";
// options.RequireHttpsMetadata = false;
// options.ApiName = "api";
// });
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.Audience = "api";
});