ASP.NET MVC WebApi2 EnableCors not work, can’t see Allow-Origin domain in Response

I have a .Net WebApi project (NOT .Net Core, PLEASE DO NOT USE .Net Core ANSWER). and i need set more origin site, first, i tried one origin for testing, but not work. (nuget: Microsoft.AspNet.Cors and Micorsoft.AspNet.WebApi.Cors, version 5.3.0)

These steps from Microsoft and Stackoverflow, and I followed to use:

Step1: Set Origin/Headers/Methods from EnableCorsAttribute globally.

Step2: Remove Web.Config system.webServer setting. (“Allow-xxxxxx” here, so I removed it.)

Step3: Client is Angular that send a request to server, and method is POST for testing.

Browser shows:

There is no origin domain in Server response, and the header is different from EnableCORS setting.

I set origin “http://localhost:4200”, method is “GET,POST,PUT,DELETE,OPTIONS” in WebApiConfig.cs for globally, Chrome show the server Response no Origin, and method is “OPTIONS, TRACE, GET, HEAD, POST”.

I need enableCORS with multiple domains and credentials, so I can’t add Header from Application_BeginRequest, because it is not support multiple domains.

protected void Application_BeginRequest()
{
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
    {
        //Response.Headers.Add("Access-Control-Allow-Origin", ConfigurationManager.AppSettings["AllowAccessDomain"]);
        Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:4200,http://localhost:4300");
    }
}

It gets CORS, not allow origin error.

My code about EnableCORS:

WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    // Web API 
    //config.EnableCors();

    EnableCorsAttribute enableCorsAttribute = new EnableCorsAttribute(
        "http://localhost:4200",
        "Origin,X-Requested-With,Content-Type,Accept,Authorization,Set-Cookie,CaptchaToken",
        "GET,POST,PUT,DELETE,OPTIONS"
    )
    {
        SupportsCredentials = true
    };

    config.EnableCors(enableCorsAttribute);

    // Web API Route
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Global.asax.cs:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Web.config

1. Remove tag: <system.webServer>

2. Remove <system.webServer> contain, only tag.

Same of message from 1 or 2, CORS error.

Chrome Browser Message:

Access to XMLHttpRequest at ‘http://localhost:9600/api/server’ from origin ‘http://localhost:4200’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

What can I do more for this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật