【发布时间】:2021-01-04 02:26:41
【问题描述】:
我正在使用托管在远程 IIS 服务器上的 .net core web api 3.1,每次删除/选项请求我都会收到 401 未经授权的 html 响应,我在启动时允许使用 CORS,但没有运气。
更新:这是我的创业公司
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BrmajaCommerceSearchContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SearchConnectionString")));
services.AddDbContext<IdentityContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddCors(o => o.AddPolicy("AllowAll", b => b.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
services.AddControllers().AddNewtonsoftJson(s =>
{
s.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Ecommerce API",
Version = "v1"
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = Assembly.GetExecutingAssembly().GetName().Name + ".xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
services.AddAuthentication()
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseCors("AllowAll");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("swagger/v1/swagger.json", "Ecommerce");
c.RoutePrefix = string.Empty;
});
}
}
【问题讨论】:
-
您介意发布 Startup.cs 吗?你的意思是它在 CUR 请求时返回 200?
-
@MichaelWang 只有 GET 和 POST 返回 200 其余返回 401,我发布了启动
-
如果应用使用身份验证/授权功能,请在
UseRouting和UseCors之后、UseEndpoints之前调用UseAuthentication和UseAuthorization。检查here。 -
@MichaelWang 这是一个 IIS 问题,因为请求没有到达它被返回 401 html 响应的 IIS 阻止的 api
-
我不明白。您可以在 UseAuthorization 之前尝试 moveup UseCors 作为我发布的链接,然后在 IIS 上重新启动您的服务。
标签: c# asp.net-core iis cors asp.net-web-api2