1

Topic: Asp.net core vs CORS

Greetings to all! I want to add own handler on POST \GET \PUT requests and it seemed to make it simply enough: Start.cs public void ConfigureServices (IServiceCollection services) {services. AddMvcCore (); services. Configure <MvcOptions> (options => {options. InputFormatters. Add (new MyInputFormatter ());}); services. AddOptions (); services. AddCors (o => o. AddPolicy ("mp", builder => {builder. AllowAnyHeader ().AllowAnyMethod ().AllowAnyOrigin ();}));} MyInputFormater.cs public class MyInputFormater: InputFormatter {static MediaTypeHeaderValue protoMediaType = MediaTypeHeaderValue. Parse ("application/mp-sarbuf"); public override bool CanRead (InputFormatterContext context) {var request = context. HttpContext. Request; MediaTypeHeaderValue requestContentType = null; MediaTypeHeaderValue. TryParse (request. ContentType, out requestContentType); if (requestContentType == null) {return false;} return requestContentType. IsSubsetOf (protoMediaType);} public override Task <InputFormatterResult> ReadRequestBodyAsync (InputFormatterContext context) {if (context == null) throw new ArgumentNullException (nameof (context)); var request = context. HttpContext. Request; var resultObject = new ProtocolIn (); if (Parse (request. Headers, ref resultObject)) {var data = Encoding. UTF8.GetString (ReadFully (context. HttpContext. Request. Body)); resultObject. Body = Convert. FromBase64String (data); return InputFormatterResult. SuccessAsync (resultObject);} return InputFormatterResult. FailureAsync ();} private bool Parse (IHeaderDictionary header, ref ProtocolIn obj) {//... Filling of fields ProtocolIn return true;} private byte [] ReadFully (Stream stream) {byte [] buffer = new byte [32768]; using (var ms = new MemoryStream ()) {while (true) {int read = stream. Read (buffer, 0, buffer. Length); if (read <= 0) return ms. ToArray (); ms. Write (buffer, 0, read);}}}} but sticks into wheels to me interposed CORS with the requests OPTIONS when it checks, and whether it is possible... And its requests with Method = "OPTIONS" are intercepted also by mine MyInputFormater. How more correctly to make that and CORS it is correct  the restrictions (while at me they are not present in an example) and did not climb in mine "MyInputFormater"? Now the first request OPTIONS does CORS, CanRead speaks return false

2

Re: Asp.net core vs CORS

Hello, codenet, you wrote: a C> Greetings to all! A C> I want to add own handler on POST \GET \PUT requests of a C> and it seemed to make it simply enough: a C> Start.cs a C> a C> public void ConfigureServices (IServiceCollection services) a C> {a C> services. AddMvcCore (); a C> services. Configure <MvcOptions> (options => a C> {a C> options. InputFormatters. Add (new MyInputFormatter ()); a C>}); a C> services. AddOptions (); a C> services. AddCors (o => o. AddPolicy ("mp", builder => a C> {a C> builder. AllowAnyHeader () a C>.AllowAnyMethod () a C>.AllowAnyOrigin (); a C>})); a C>} a C> a C> MyInputFormater.cs a C> a C> public class MyInputFormater: InputFormatter a C> {a C> static MediaTypeHeaderValue protoMediaType = MediaTypeHeaderValue. Parse ("application/mp-sarbuf"); a C> public override bool CanRead (InputFormatterContext context) a C> {a C> var request = context. HttpContext. Request; C> MediaTypeHeaderValue requestContentType = null; A C> MediaTypeHeaderValue. TryParse (request. ContentType, out requestContentType); A C> if (requestContentType == null) A C> {A C> return false; A C>} A C> return requestContentType. IsSubsetOf (protoMediaType); A C>} A C> public override Task <InputFormatterResult> ReadRequestBodyAsync (InputFormatterContext context) A C> {A C> if (context == null) A C> throw new ArgumentNullException (nameof (context)); A C> var request = context. HttpContext. Request; A C> var resultObject = new ProtocolIn (); A C> if (Parse (request. Headers, ref resultObject)) A C> {A C> var data = Encoding. UTF8.GetString (ReadFully (context. HttpContext. Request. Body)); A C> resultObject. Body = Convert. FromBase64String (data); A C> return InputFormatterResult. SuccessAsync (resultObject); A C>} A C> return InputFormatterResult. FailureAsync (); C>} a C> private bool Parse (IHeaderDictionary header, ref ProtocolIn obj) a C> {a C>//... Filling of fields ProtocolIn a C> return true; a C>} a C> private byte [] ReadFully (Stream stream) a C> {a C> byte [] buffer = new byte [32768]; a C> using (var ms = new MemoryStream ()) a C> {a C> while (true) a C> {a C> int read = stream. Read (buffer, 0, buffer. Length); a C> if (read <= 0) a C> return ms. ToArray (); a C> ms. Write (buffer, 0, read); the C>} a C>} a C>} a C>} a C> a C> but sticks to me interposed into wheels CORS with the requests OPTIONS a C> when it checks, and whether it is possible... Cs> and its requests with Method = "OPTIONS" are intercepted also by mine MyInputFormater. A C> How more correctly to make that and CORS it is correct  the restrictions (while at me them is not present in an example) a C> and did not climb in mine "MyInputFormater"? The C> now does the first request OPTIONS CORS, the C> CanRead speaks return false understood like, I thought that hanging up attribute [EnableCors ("mp")] on the controler all will work, it was necessary still public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {app. UseCors ("mp");}