0

I am facing one issue of CORS policy. Please note that I am not using web API or .NET core application where we have library to enable cors. I am using MVC application of .Net framework 4.8 Here is my step to call other resource from my MVC application. 1 - calling search action controller d.cs using ajax call from jquery 2 - checking request authentication in search action of d.cs 3 - if request is not valid then redirect to SignI action of home controller. 4 - my SignIn action redirect to Microsoft site https://login.microsoftonline.com to authenticate the request. I implemented microsoft OWIN.

enter image description here

Please note - this is working fine when my request is not originate from ajax/jquery.

I gone through various solutions provided, but no success Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method

I did following changes 1 - In web.config

<httpProtocol>
    <customHeaders>
      <clear />
      <add name="Access-Control-Allow-Origin" value="*" />
   </customHeaders>
</httpProtocol>

2 - created custom action filter and apply over my controller

public class CustomActionFilter : ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow- 
     Origin", "*");
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow- 
     Headers", "Origin, X-Requested-With, Content-Type, Accept");
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow- 
     Credentials", "true");
        if (filterContext.HttpContext.Request.HttpMethod == "OPTIONS")
        {
            filterContext.HttpContext.Response.Flush();
        }

        base.OnActionExecuting(filterContext);
    }
  }
m.k.sharma
  • 155
  • 1
  • 2
  • 12
  • Issue resolved, I changed the approach to authenticate request. I created custom authentication filter and apply on controller level, my previous approach to redirect user to login controller from every controller's action if request is not authenticated. Please note - In my previous approach, redirection happen like from jquery ajax -> search controller(d.cs) -> login controller -> microsite authentication. which was create CORS issue. – m.k.sharma Jun 27 '23 at 11:24

0 Answers0