Asp.net core 服务器无法授权它';他有自己的代币
这里是设置,我有一个身份验证服务器,它向一个angular网站发布令牌。我在AuthServer中有一个控制器,它需要使用[Authorize]系统来只允许有效的JWT令牌。当我检查控制器中的Asp.net core 服务器无法授权它';他有自己的代币,asp.net-core,openid-connect,Asp.net Core,Openid Connect,这里是设置,我有一个身份验证服务器,它向一个angular网站发布令牌。我在AuthServer中有一个控制器,它需要使用[Authorize]系统来只允许有效的JWT令牌。当我检查控制器中的User变量时,它始终为空,但当我检查控制器上的HttpRequestHeader时,我看到正在发送令牌 我还有一个Api服务器,我使用JWT令牌和[Authorize]系统很容易实现它。 另一层,我在docker容器中运行Api和Auth服务器 AuthServer中的我的整个Startup.cs文件:
User
变量时,它始终为空,但当我检查控制器上的HttpRequestHeader时,我看到正在发送令牌
我还有一个Api服务器,我使用JWT令牌和[Authorize]系统很容易实现它。
另一层,我在docker容器中运行Api和Auth服务器
AuthServer中的我的整个Startup.cs文件:
var connectionString = Configuration.GetConnectionString("Default");
if (_env.IsDevelopment())
{
try
{
using (AppIdentityDbContext identityDb =
new AppIdentityDbContextFactory(connectionString).Create())
{
int Pendings = identityDb.Database.GetPendingMigrations().Count();
identityDb.Database.Migrate();
}
using (PersistedGrantDbContext persistGrantDb =
new PersistedGrantDbContextFactory(connectionString).Create())
{
int Pendings = persistGrantDb.Database.GetPendingMigrations().Count();
persistGrantDb.Database.Migrate();
}
}
catch (Exception)
{
}
}
services.AddControllersWithViews();
services.AddDbContextPool<AppIdentityDbContext>(options => options.UseSqlServer(connectionString));
services
.AddIdentity<AppUser, IdentityRole>(config=> {
config.User.RequireUniqueEmail = true;
config.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer().AddDeveloperSigningCredential()
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(Configuration.GetConnectionString("Default"));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = (int)TimeSpan.FromDays(1).TotalSeconds; // interval in seconds
})
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<AppUser>()
.AddProfileService<AppUserProfileService>()
.AddJwtBearerClientAuthentication();
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme,
jwtOptions =>
{
// jwt bearer options
jwtOptions.Authority = _env.IsDevelopment() ? "https://localhost:5001" : "";
jwtOptions.RequireHttpsMetadata = _env.IsDevelopment() ? false : true;
jwtOptions.Audience = "resourceapi";
jwtOptions.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateAudience = false,
ValidateIssuer = _env.IsDevelopment() ? false : true,
ValidateActor = false,
ValidateIssuerSigningKey = false
};
},
referenceOptions =>
{
// oauth2 introspection options
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
services.AddSingleton<IEmailSender, SmtpSender>();
检查AccountController:Controller中的用户
var u = User;
var _user = await _userManager.GetUserAsync(u);
var e = this._httpContextAccessor;
var u = User;
var _user = await _userManager.GetUserAsync(u);
var e = this._httpContextAccessor;