Hello,
I have one VAPT issue pending, of CORS misconfiguration. I have defined Access-Control-Allow-Origin with * in my web.config
How to prevent or fix this CORS misconfiguration in project?
Project is in mvc web api
Thank You !
Hello,
I have one VAPT issue pending, of CORS misconfiguration. I have defined Access-Control-Allow-Origin with * in my web.config
How to prevent or fix this CORS misconfiguration in project?
Project is in mvc web api
Thank You !
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted Apr 10, 2024, 11:01 PM EDT
Instead of using
value="*", specify the allowed origins explicitly. For example, if you want to allow requests fromhttp://example.comandhttps://example.com, you would modify thevalueattribute as follows:By explicitly specifying the allowed origins, you provide a more secure configuration compared to allowing all origins (
*). This helps mitigate the risk of unauthorized access to your API resources.Remember to thoroughly test your API endpoints after making these changes to ensure that CORS is correctly configured and that your API behaves as expected for different origin requests.
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 11, 2024, 12:17 AM EDT
The issue you’re facing is due to the Access-Control-Allow-Origin header being set to “*”. This allows any domain to access your API, which can be a security risk.
To fix this, you should specify the exact domains that are allowed to access your API. Here’s how you can do it:
protected void Application_BeginRequest() { var allowedOrigins = new List { "http://domain1.com", "http://domain2.com" }; var origin = HttpContext.Current.Request.Headers["Origin"]; if (origin != null && allowedOrigins.Contains(origin)) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true"); } }Remember to replace “http://specific-domain.com”, “http://domain1.com”, and “http://domain2.com” with the actual domains that you want to allow.
Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 11, 2024, 6:07 AM EDT
To prevent or fix CORS misconfiguration in your MVC Web API project, you can handle CORS settings directly in your code rather than relying solely on the web.config file. In your Web API project, you can enable CORS globally by installing the
Microsoft.AspNet.WebApi.Corspackage and then configuring CORS in theWebApiConfig.csfile.using System.Web.Http; using System.Web.Http.Cors; namespace YourProjectNamespace { public static class WebApiConfig { public static void Register(HttpConfiguration config) { var cors = new EnableCorsAttribute("*", "*", "*"); config.EnableCors(cors); // Other Web API configuration code } } }Lalji DhameliyaRank #3,336 · 431 reputationPosted Apr 12, 2024, 8:30 AM EDT
To prevent CORS misconfiguration in your ASP.NET MVC Web API project, you can create a custom action filter attribute to add the necessary CORS headers to the response. Here's a concise example:
using System.Web.Mvc; public class AllowCorsAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); base.OnActionExecuting(filterContext); } }Apply this attribute to your controllers or actions where CORS needs to be enabled:
[AllowCors] public class HomeController : Controller { // actions }This approach ensures that the
Access-Control-Allow-Originheader is included in the response, allowing cross-origin requests from any origin.