Tag: cors

MVC Web API错误代码SCRIPT7002

作为参考,我使用的是Windows 10和Visual Studio 2017。 我有一个MVC web api和相应的Web应用程序,并具有登录function。 我只是尝试使用IE进行调试。 每当我使用GET调用时,我遇到以下错误:SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。 我已经配置并启用了CORS,所以我完全不知道为什么这不起作用。 在我的WebApiConfig文件中,我有这个: public static void Register(HttpConfiguration config) { // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional } ); config.EnableCors(); […]

Azure移动应用程序 – 尝试POST时获取405(方法不允许)

我正在尝试将Azure Mobile Service .NET后端迁移到Azure移动应用程序。 我使用的是一些自定义Web Api控制器,迁移后我得到405 (Method Not Allowed) / The requested resource does not support http method ‘POST’. 尝试POST到以前工作的控制器方法时出错。 我花了几个小时尝试不同的CORS设置但到目前为止我没有成功。 这就是我目前配置Web Api的方式: HttpConfiguration config = new HttpConfiguration(); new MobileAppConfiguration() .UseDefaultConfiguration() .ApplyTo(config); var cors = new EnableCorsAttribute(“*”, “*”,”*”); //var cors = new EnableCorsAttribute(“*”, “*”,”GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS”); config.EnableCors(cors); config.Routes.MapHttpRoute( name: “Rest”, routeTemplate: “rest/{controller}/{id}”, defaults: new { id = […]

服务包含来自MVC / IIS Web应用程序的子域

我在tumblr上为我的网站托管博客: blog.withoomph.com 我在博客上修改了主题,我想使用我在主站点上使用的自己的字体。 我想从以下字体获取字体: beta.withoomph.com 但是,当页面尝试获取字体时,我收到CORS错误。 这种情况最近才开始出现,因为它们可以毫无问题地获取。 有没有办法我可以设置IIS或我的MVC应用程序来将这些字体服务到子域? 编辑 对不起,我应该知道的比这更好。 以下是一些更多信息: 这是获取url的CSS: @font-face { font-family: ‘avantgarde’; src: url(‘http://beta.withoomph.com/content/fonts/avant_garde.eot’); src: url(‘http://beta.withoomph.com/content/fonts/avant_garde.eot?#iefix’) format(’embedded-opentype’), url(‘http://beta.withoomph.com/content/fonts/avant_garde.woff’) format(‘woff’), url(‘http://beta.withoomph.com/content/fonts/avant_garde.ttf’) format(‘truetype’), url(‘http://beta.withoomph.com/content/fonts/avant_garde.svg#avantgarde_bk_btdemi’) format(‘svg’); font-weight: normal; font-style: normal; } 这是尝试获取字体时开发控制台中的错误: 来自“ http://beta.withoomph.com ”的字体已被跨源资源共享策略阻止加载:请求的资源上没有“Access-Control-Allow-Origin”标头。 因此,不允许来源“ http://blog.withoomph.com ”访问。

HTTP代码405(方法不允许),当我在ASP.NET WebAPI CORS中发送POST数据时

Web.config文件 调节器 [EnableCors(origins: “http://domain/api/Clients”, headers: “*”, methods: “*”)] public class ClientsController : ApiController { private ParaNewnergyEntities db = new ParaNewnergyEntities(); // GET: api/Clients public IEnumerable GetClient() { return db.Client.ToList(); } [HttpPost] public IHttpActionResult PostClient(String Username, String Password) { Client c = new Client { Username = Username, Password = Password }; db.Client.Add(c); db.SaveChanges(); return Ok(c); […]

将base64字符串发送到c#server

我现在制作了ac#web api。 它工作正常,直到今天。 我试图将图像转换为base64string,然后通过ajax将base64string发送到c#服务器。 当我执行上述步骤时,会发生错误。 XMLHttpRequest cannot load http://10.0.10.105:50231/api/hello. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://10.0.10.201’ is therefore not allowed access. 我不知道主要问题存在于哪里,但在我的观察中,只有在将非常长的base64string传递给服务器时才会出现错误,因为当我尝试发送短测试字符串时问题不会出现并且一切正常。 你知道解决这个问题的更好方法是什么? 或任何其他方式来实现我所需的目标? 我的ajax代码是这样的。 $.ajax({ type: ‘POST’, //GET or POST or PUT or DELETE verb url: ‘http://10.0.10.105:50231/api/hello’, // Location of the service contentType: ‘application/x-www-form-urlencoded’, // content type sent to server […]

WebAPI CORS和Ninject

我有一个具有ninject的ac #WebAPI项目,所有的get函数都在工作。 但是,每当我尝试发布时,我都会从api获得“Method Not Allowed”响应。 我已经阅读了一些地方,这是因为当api拒绝交叉来源呼叫时。 所以我使用了包管理器控制台并安装了Microsoft.AspNet.WebApi.Cors nugent包,并按照文章http://www.asp.net/web-api/overview/security/enabling-cross-origin的说明进行操作。 -requests-in-web-api 。 我的问题是现在Ninject不工作但抛出以下内容:尝试通过方法’System.Web.Http.GlobalConfiguration..cctor()’来访问字段’System.Web.Http.GlobalConfiguration.CS $ 9__CachedAnonymousMethodDelegate2’失败。“} 有人能帮我一下吗? 抛出错误的行是:System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); Ninject配置文件 public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// /// Starts the application /// public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// /// Stops the application. /// public static […]

为ASP .NET MVC中的静态资源启用CORS?

我在Web API和ASP .NET MVC中的通用控制器中找到了大量有关CORS的资源。 但是,我的情况是,我希望特定文件夹中的所有静态资源(CSS和JS文件)也可以通过AJAX下载。 换句话说,为这些资源或该文件夹启用CORS。 我怎么能做到这一点? 我没有发现类似的问题。 它们都与Web API或通用控制器相关。

如何在ASP.NET MVC中启用跨源请求

我正在尝试创建一个Web应用程序,它在MVC 5中使用跨源请求(CORS)。我已经尝试了所有内容而没有任何结果。 带有属性 public class AllowCrossSiteJsonAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.RequestContext.HttpContext.Response.AddHeader(“Access-Control-Allow-Origin”, “*”); base.OnActionExecuting(filterContext); } } 使用EnableCors属性 [EnableCors(“*”)] 什么都行不通我开始认为这是不可能的

.NET Core UseCors()不添加标头

这将是Access-Control-Allow-Origin标头如何工作的重复? ,但那里的方法也不适合我。 我希望我只是缺少一些东西。 我试图从我的.NET Core Web API的响应中获取一个Access-Control-Allow-Origin标头,我通过AJAX访问它。 我尝试过几件事。 除非另有说明,否则所有内容都在Startup.cs文件中。 方法1 根据Microsoft文档 : public void ConfigureServices(IServiceCollection services) { // Add database services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString(“DbConnection”))); // Add the ability to use the API with JSON services.AddCors(); // Add framework services. services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection(“Logging”)); loggerFactory.AddDebug(); if (env.IsDevelopment()) { using […]

在Asp.net Web API中处理CORS预检

我的架构中有三个应用程序。 它们位于同一台服务器上,但具有不同的端口号。 A – Token Application (port 4444) – Asp.net WebApi B – API Application (port 3333) – Asp.net WebApi C – UI Application (port 2222) – AngularJS App. 申请流程如下 1- UI项目从令牌应用程序获取令牌(它需要Windows awxrsdsaWeffs12davalidation。)例如: awxrsdsaWeffs12da 2- UI应用程序将此标记放入自定义标头,该标头名为“accessToken” 例如:accessToken: awxrsdsaWeffs12da 3- UI应用程序向API Application Ex发送请求: http:myaddress:3333/api/TheRestServiceHere UI应用程序获得401错误。 哪个发送OPTIONS方法。 (我想预检问题) 在我的web api项目中,我启用了如下所示的Cors。 public static void Register(HttpConfiguration config) { …. […]