Hello,
We have a use case where we send information from a website to creatio via custom code. Landing page was not suited for our usecase.
We successfully implemented this using Anonymous Service which has code to manage CORS. We need to make the service work for .net core now. How do we transfer below code to .net core setup since the name 'WebOperationContext' does not exist in .net Core? What is the best practice to overcome CORS error in .net core setup?
// Preflight [OperationContract] [WebInvoke(Method = "OPTIONS", UriTemplate = "*")] public void GetCaptchaPreflight() { var outgoingResponseHeaders = WebOperationContext.Current.OutgoingResponse.Headers; outgoingResponseHeaders.Add("Access-Control-Allow-Origin", "*"); outgoingResponseHeaders.Add("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); outgoingResponseHeaders.Add("Access-Control-Allow-Headers", "*"); outgoingResponseHeaders.Add("Access-Control-Request-Headers", "X-Requested-With, x-request-source, accept, content-type"); } } // Request endpoint [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] public void ConfirmCaptcha(Stream responseDetails) { var logger = global::Common.Logging.LogManager.GetLogger("CaptchaService"); logger.Info("Captcha services entered :"); var currentWebOperationContext = WebOperationContext.Current; var outgoingResponseHeaders = currentWebOperationContext.OutgoingResponse.Headers; outgoingResponseHeaders.Add("Access-Control-Allow-Origin", "*"); outgoingResponseHeaders.Add("Access-Control-Allow-Methods", "POST"); outgoingResponseHeaders.Add("Access-Control-Allow-Headers", "Origin, Content-Type, Accept"); try { StreamReader reader = new StreamReader(responseDetails); string responseJson = reader.ReadToEnd(); logger.Info("Captcha services :" + responseJson); ResponseDetails rd = new ResponseDetails(); rd = JsonConvert.DeserializeObject<ResponseDetails>(responseJson); if (!string.IsNullOrEmpty(rd.GToken)) { if (IsCaptchaVerified(rd.GToken)) { logger.Info("Captcha is valid"); logger.Info(rd.LeadId); logger.Info(rd.ContactId); logger.Info(rd.LeadResponse); } } else { logger.Info("Captcha is invalid"); } }
Like
1 comments
11:45 Apr 19, 2022
Hello,
As for now we don't have best practices for CORS management for .NET Core however you need to try testing all the recommendations described here (they are for .NET Core).
Best regards,
Oscar
Show all comments