javascript - Asp.NET Web API 和 SignalR Cors

标签 javascript asp.net asp.net-web-api signalr cors

我正在开发一个 DevExtreme 项目,我正在使用 Web API 和 SignalR。我有两个项目作为 Asp.NET Web API 和 DevExtreme。我在有关 cors 的 call.js 文件上有错误。我的代码如下:

http://localhost:40623/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22notificationshub%22%7D%5D&_=1431708909660. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:50847' is therefore not allowed access.

WebApiConfig 文件(Web API 项目):

config.MapHttpAttributeRoutes();

config.EnableCors();

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

OWIN 启动文件(Web API 项目):

public void Configuration(IAppBuilder app)
{
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    ConfigureAuth(app);

    GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new UserIdProvider());

    //If I uncomment this line, then error will be:
    //The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:50847, *', but only one is allowed. Origin 'http://localhost:50847' is therefore not allowed access.
    //app.UseCors(CorsOptions.AllowAll);
    app.MapSignalR();
}

Web.Config 文件(Web API 项目):

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <clear />
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Authorization" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

Calls.js 文件(DevExtreme 项目):

$.connection.hub.url = "http://localhost:40623/signalr";
var notificationsHub = $.connection.notificationsHub;
notificationsHub.client.newCall = function (message) {
    viewModel.calls.load();
};
$.connection.hub.start();

最佳答案

您需要将其添加到您的服务器配置中:

Access-Control-Allow-Origin: ORIGIN
Access-Control-Allow-Credentials: false
Access-Control-Allow-Methods: ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL
Access-Control-Allow-Headers: Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If
Access-Control-Expose-Headers: DAV, content-length, Allow

如果您使用 Apache,则配置文件名为 httpd.conf。

Access-Control-Allow-Origin: ORIGIN 

也可以

Access-Control-Allow-Origin: *

Access-Control-Allow-Origin: ORIGIN | ORIGIN2 | ...

关于javascript - Asp.NET Web API 和 SignalR Cors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30265112/

相关文章:

javascript - 如何解决 Nestjs 中的 "Strategy#authenticate must be overridden by subclass"错误

c# - 运行时 ASP.net 的 IIS 应用程序名称

c# - ASP.NET Core (MVC 6) 中的路由消歧

javascript - 使用不同的 props 在容器中多次 react 渲染组件?

javascript - 如何分析和减小 browserify 生成的文件的大小?

asp.net - 每个配置文件的部分只能出现一次。请参阅帮助主题 <location> 了解异常(exception)情况。为什么?

asp.net - 将图片存储为文件或存储在数据库中,例如用于 Web 应用程序的 MSSQL?

c# - 如何从 url Aspnet WebApi 读取字符串参数和表单数据

c# - 如何将 zip 文件发送到 ASP.NET WebApi

javascript - Selenium WebDriver findElements 返回对象