我目前正在建立一个Duende IdentityServer,以保护我的网络应用程序和API。最初,我的API与我的IdentityServer在同一个应用程序中。我正在努力把两者分开。目前,我的IdentityServer部署在Azure。我目前使用的设计是
(LocalHost)
我遇到的问题是,即使有一个有效的标记,我也无法使基本的授权属性正常工作。它不断地返回401 -未经授权。
我从我自己的狩猎中得到了证实:
我正在正确地将访问API的范围添加到jwt.
app.UseAuthenticate()在 CORS之前接受所有的起源、方法和头文件。我完全不知所措,我似乎弄不清我配置错了什么。我也在使用API中的实体框架来开发数据库。该数据库上下文继承自IdentityDbContext。我认为这不会影响身份验证,因为我不会在启动时添加身份。
我的IdentityServer在将IdentityServer添加到web应用程序时确实调用了IdentityServer。我是否需要在我的API/Webapp中获得签名的凭据?
这是我的API的启动。
//Initializing WebApp Adding Razor page support and controller support
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
// base-address of your identityserver
options.Authority = builder.Configuration["identity-server-url"];
options.Audience = "LMS.API";
// audience is optional, make sure you read the following paragraphs
// to understand your options
options.TokenValidationParameters = new TokenValidationParameters
{
RoleClaimType = ClaimsIdentity.DefaultRoleClaimType,
};
});
builder.Services.AddCors(confg =>
confg.AddPolicy("AllowAll",
p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
//Add Key Vault to configuration to be used in entire application
var secretClient = new SecretClient(new(builder.Configuration["KeyVault:Endpoint"]), new DefaultAzureCredential());
builder.Configuration.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
var migrationsAssembly = typeof (Program).Assembly.FullName;
//Grab connection string from key vault
string dbConnectionString = builder.Configuration["db-connection-string"];
// Repository registration
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IAssignmentRepository, AssignmentRepository>();
builder.Services.AddScoped<ICourseRepository, CourseRepository>();
builder.Services.AddScoped<ISchoolRepository, SchoolRepository>();
//Auto Generated shit
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//add support for MediatR
builder.Services.AddMediatR(typeof(CreateAssignmentForCourse).Assembly);
builder.Services.AddMediatR(typeof(Program).Assembly);
//Injecting DB connection string into Master Context class,
builder.Services.AddDbContext<MasterContext>(options =>
{
options.UseSqlServer(dbConnectionString, sqlServerOptionsAction: sqloptions =>
{
sqloptions.EnableRetryOnFailure();
});
});
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddControllers();
//Build webApp
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(routes =>
{
routes.MapControllers();
});
app.Run();发布于 2021-12-31 15:08:56
很抱歉没有及时回复。我在这里解决了这个问题。我试图验证听众,以匹配我为API创建的API范围。但是,通过手动更改db表,我手动配置了API范围。因此,长话短说,我的JWT中的受众范围为null,不匹配,因此导致了未经授权的HTTP响应。
希望这对未来的人有帮助!
https://stackoverflow.com/questions/70423425
复制相似问题