The beta version of CORS succeeded in installing the following:
services.AddMvc();
services.ConfigureCors(options =>
{
});
app.UseCors();
app.UseMvc();
It worked like a charm, but a beta break is ripping it apart. I found these questions: Why does Cors not work after upgrading to beta8 on ASP.NET 5? and fixed as follows:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.AllowAnyOrigin();
builder.AllowCredentials();
});
});
services.AddMvc();
app.UseMvc();
[EnableCors("CorsPolicy")]
public IActionResult Get()
{
return OK();
}
Yes, it works again, but when I add [Authorize("Bearer")]
, the controller returns 401 Unauthorized for the OPTIONS request via an ajax call. Here is the HTTP request and response.
[Request]
OPTIONS https://api.mywebsite.net/ HTTP/1.1
Accept: */*
Origin: https://myanotherwebsite.net
Access-Control-Request-Method: GET
Access-Control-Request-Headers: accept, authorization
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
Host: api.mywebsite.net
Connection: Keep-Alive
Cache-Control: no-cache
[Answer]
HTTP/1.1 401 Unauthorized
Content-Length: 0
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=...;Path=/;Domain=api.mywebsite.net
Date: Fri, 23 Oct 2015 09:56:34 GMT
How to enable CORS with attribute [Authorization]
in ASP.NET 5 beta8?
Edit
ASP.NET MV C ( 8).
[EnableCors]
, [Authorize]
, 401 Unauthorized ( 302 ).
Edit2
, . , .