首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用[ .NET ]属性时,带有的Web返回401

使用[ .NET ]属性时,带有的Web返回401
EN

Stack Overflow用户
提问于 2021-12-20 14:35:38
回答 1查看 1.3K关注 0票数 0

我目前正在建立一个Duende IdentityServer,以保护我的网络应用程序和API。最初,我的API与我的IdentityServer在同一个应用程序中。我正在努力把两者分开。目前,我的IdentityServer部署在Azure。我目前使用的设计是

(LocalHost)

  • API在使用IdentityServer (托管在Azure中)时签署
  1. WebApp,
  2. WebApp调用API并在请求中发送Bearer令牌,有更多的身份验证来针对IdentityServer.(LocalHost)进行验证

我遇到的问题是,即使有一个有效的标记,我也无法使基本的授权属性正常工作。它不断地返回401 -未经授权。

我从我自己的狩猎中得到了证实:

我正在正确地将访问API的范围添加到jwt.

  • 中,以确保我的app.UseAuthenticate()在 CORS之前接受所有的起源、方法和头文件。

我完全不知所措,我似乎弄不清我配置错了什么。我也在使用API中的实体框架来开发数据库。该数据库上下文继承自IdentityDbContext。我认为这不会影响身份验证,因为我不会在启动时添加身份。

我的IdentityServer在将IdentityServer添加到web应用程序时确实调用了IdentityServer。我是否需要在我的API/Webapp中获得签名的凭据?

这是我的API的启动。

代码语言:javascript
复制
//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();
EN

回答 1

Stack Overflow用户

发布于 2021-12-31 15:08:56

很抱歉没有及时回复。我在这里解决了这个问题。我试图验证听众,以匹配我为API创建的API范围。但是,通过手动更改db表,我手动配置了API范围。因此,长话短说,我的JWT中的受众范围为null,不匹配,因此导致了未经授权的HTTP响应。

希望这对未来的人有帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70423425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档