
Мой вопрос:
У меня есть ASP.net Core WebApi для авторизовать и аутентифицировать пользователей (.net 7). Я столкнулся с проблемой CORS при вызове конечной точки входа в этот API с помощью приложения Angular:
Код: Выделить всё
Access to fetch at 'https://myAPI.azurewebsites.net/accounts/authenticate' from origin 'https://myAPP.azurewebsites.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
Код: Выделить всё
login(email: string, password: string) {
return this.http.post(`${baseUrl}/authenticate`, { email, password }, { withCredentials: true })
.pipe(map(account => {
this.accountSubject.next(account);
this.startRefreshTokenTimer();
return account;
}));
}
Код: Выделить всё
var builder = WebApplication.CreateBuilder(args);
// add services to DI container
{
var services = builder.Services;
var env = builder.Environment;
services.AddDbContext();
services.AddCors();
services.AddControllers().AddJsonOptions(x =>
{
// serialize enums as strings in api responses (e.g. Role)
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddSwaggerGen();
// configure strongly typed settings object
services.Configure(builder.Configuration.GetSection("AppSettings"));
// configure DI for application services
services.AddScoped();
services.AddScoped();
services.AddScoped();
builder.Services.Configure x.SwaggerEndpoint("/swagger/v1/swagger.json", ".NET Sign-up and Verification API"));
app.UseCors(x => x
.SetIsOriginAllowed(origin => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
// global error handler
app.UseMiddleware();
// custom jwt auth middleware
app.UseMiddleware();
app.MapControllers();
app.Run(builder.Configuration["AppSettings:origins"]);
Я также пытался настроить свою собственную политику CORS:
Код: Выделить всё
.
.
.
// global cors policy
app.UseCors(
policy =>
{
policy.WithOrigins
("http://localhost:8100",
"https://myAPP.azurewebsites.net",
"https://API1.azurewebsites.net",
"https://API2.azurewebsites.net",
"https://www.myDomain.de")
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithMethods("GET", "PUT", "POST", "DELETE", "OPTIONS");
}
);
.
.
.

См. описание выше.< /п>
Подробнее здесь: https://stackoverflow.com/questions/762 ... ndpoint-of