Mesmo registrando um usuário e logando com ele, ao testar o get artista ele volta 401.
Login:
Método:
Mesmo registrando um usuário e logando com ele, ao testar o get artista ele volta 401.
Login:
Método:
program.cs
using Microsoft.EntityFrameworkCore;
using ScreenSound.API.Endpoints;
using ScreenSound.Data;
using ScreenSound.Modelos;
using ScreenSound.Shared.Dados.Modelos;
using ScreenSound.Shared.Models.Models;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
uilder.Services.AddCors(
options => options.AddPolicy(
"wasm",
policy => policy.WithOrigins(
[builder.Configuration["BackendUrl"] ?? "https://localhost:7122/",
builder.Configuration["FrontendUrl"] ?? "https://localhost:7199/"]
).AllowAnyMethod()
.SetIsOriginAllowed(pol => true)
.AllowAnyHeader()
.AllowCredentials()
)
);
builder.Services.AddDbContext<ScreenSoundContext>((options) =>
{
options.UseSqlServer(builder.Configuration["ConnectionStrings:ScreenSoundDB"]).UseLazyLoadingProxies(false);
});
builder.Services.AddIdentityApiEndpoints<PessoaComAcesso>().AddEntityFrameworkStores<ScreenSoundContext>();
builder.Services.AddAuthorization();
builder.Services.AddTransient<DAL<Artista>>();
builder.Services.AddTransient<DAL<Musica>>();
builder.Services.AddTransient<DAL<Genero>>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>
(
options => options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles
);
var app = builder.Build();
app.UseAuthentication();
app.UseCors("wasm");
app.AddEndPointArtistas();
app.AddEndPointMusicas();
app.AddEndPointGenero();
app.MapGroup("auth").MapIdentityApi<PessoaComAcesso>().WithTags("Autorizar");
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors(x => x.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true)
.AllowCredentials()
);
app.UseStaticFiles();
app.Run();
Ainda não consegui encontrar o bug.
Olá Igor, bom dia camarada!
Parece que você está enfrentando um problema de autenticação ao tentar acessar o endpoint de artistas, recebendo um erro 401. Aqui estão algumas sugestões para resolver isso:
Verifique o Token de Autenticação:
/Artistas
. O erro 401 geralmente indica que o servidor não recebeu credenciais válidas.Cookies e Headers:
Authorization
como Bearer <token>
.Configuração do Endpoint:
RequireAuthorization()
. Isso significa que ele requer autenticação. Certifique-se de que a autenticação está configurada corretamente no Program.cs
e que o serviço de identidade está sendo usado conforme necessário.app.UseAuthorization();
-Verifique o Login:
Debugging:
Espero que ajude e bons estudos!
Boa tarde professor.
O que estava faltando era o app.UseAuthorization(); no meu program.cs da API.
Confundi o app.UseAuthentication(); com o app.UseAuthorization(); e achei que também já tinha colocado ele no meu arquivo.