CORS endpoints on asp.net Webforms [WebMethod] endpoints

后端 未结 6 1170
北海茫月
北海茫月 2021-01-04 06:47

I am trying to add some [WebMethod] annotated endpoint functions to a Webforms style web app (.aspx and .asmx).

I\'d like to annotate those endpoints w

6条回答
  •  清歌不尽
    2021-01-04 07:03

    If you need the preflight request, e.g. so you can send authenticated requests, you are not able to set Access-Control-Allow-Origin: *. It must be a specific Origin domain.
    Also you must set the Access-Control-Allow-Methods and Access-Control-Allow-Headers response headers, if you are using anything besides the defaults.
    (Note these constraints are just how CORS itself works - this is how it is defined.)

    So, it's not enough to just throw on the [EnableCors] attribute, you have to set values to the parameters:

    [EnableCors(origins: "https://www.olliejones.com", headers: "X-Custom-Header", methods: "PUT", SupportsCredentials = true)]
    

    Or if you want to do things manually and explicitly:

    HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "https://www.olliejones.com");
    HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers", "X-Custom-Header");
    HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "PUT");
    HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
    

    One last thing - you do have to call .EnableCors() on initiation. In e.g. MVC or WebAPI, you would call this on HttpConfiguration, when registering the config and such - however I have no idea how it works with WebForms.

提交回复
热议问题