1

我正在运行SenseNet 7.0.0,正在运行它,ASP.NET 5.2.3并且正在尝试从 Angular (Typescript) 应用程序运行 api 调用。Angular 应用程序运行在localhost:4200ASP.NET 应用程序上localhost:55064。我按照本教程安装 Sensenet 并使用本教程安装网页。

当我运行 api 调用时,我收到此错误:

The 'Access-Control-Allow-Origin' header has a value 'http://localhost:55064' that is not equal to the supplied origin. Origin 'http://localhost:4200' is therefore not allowed access.

在 Content Explorer 中,我导航到 Root/System/Settings/Portal.settings。在设置中,我在文件底部添加了下一个代码:

,
AllowedOriginDomains: [ "http://localhost:4200", "http://localhost:55064/" ]

我也试过用[ "*" ]and[ "localhost" ]而不是两个本地主机。是portal.properties 文件的屏幕截图。更改值后,我没有忘记单击保存按钮。我预计这会解决问题,但事实并非如此。即使它不应该涉及重新启动,我也尝试重新启动 asp.net 项目和服务器。那也没有解决问题。我尝试了这些解决方案,因为sensenet wikisensenet 文档声明应将外部应用程序的 url 添加AllowedOriginDomains到白名单中。

当我尝试使用外部程序访问 API 时,如何解决上述错误?


我不认为 Angular 调用是这里的问题,但以防万一:

进口声明:

import {HttpClient} from '@angular/common/http';

HttpClient注入:

constructor(private http: HttpClient) {
}

角度 API 调用:

testApiCall() {
  this.http.post(
    Configuration.ServerWithApiUrl + '/Odata.svc/(\'Root\')/Login?metadata=no',
    '"username" : "admin", "password" : "admin"')
    .subscribe(data => this.callResult = data);
}

这是错误的另一次:

The 'Access-Control-Allow-Origin' header has a value 'http://localhost:55064' that is not equal to the supplied origin. Origin 'http://localhost:4200' is therefore not allowed access.

这是从 .asp.net 项目运行的 ajax 调用localhost:55064。这显示了成功消息。当我从独立的 html 文件运行它时,它还会显示成功消息。当我从独立文件运行它时,它也会显示错误。在错误而不是“localhost:4200”中,它显示“null”。

function testLoginCall() {
    $.ajax({
        url: "/Odata.svc/('Root')/Login?metadata=no",
        dataType: "json",
        type: 'POST',
        data: JSON.stringify({
            'username': "admin",
            'password': "admin"
        }),
        success: function (d) {
            console.log('You are logged in!');
        }
    });
}
4

4 回答 4

1

It turns out this is a bug or limitation in Sensenet 7.0.0. You can see the status here.

For now, a workaround is to build the Angular project using ng build --base-href=~/Scripts/Angular and paste the contents of the dist folder inside the /Scripts/Angular folder in the ASP.NET project. Then, replace the contents of the _Layout.cshtml file with the contents of the index.html file from the dist folder, put back the @RenderBody() in the _Layout.cshtml and run the ASP.NET project

Both these api calls now work using the workaround:

testLoginApiCall() {
  this.http.post(
    Configuration.ServerWithApiUrl + '/Odata.svc/(\'Root\')/Login?metadata=no',
    '{"username" : "admin", "password" : "admin"}')
    .subscribe(
      data => console.log('Succes!', data),
      error => console.log('An error occured.', error));
}

testCreateWorkspaceCall() {
  this.http.post(
    Configuration.ServerWithApiUrl + 'OData.svc/(\'Root\')',
    'models=[{"__ContentType": "Workspace", "DisplayName": "Workspace"}]')
    .subscribe(
      data => console.log('Succes!', data),
      error => console.log('An error occured.', error));
}
于 2018-01-25T15:20:26.093 回答
1

添加"localhost"到设置文件中允许的来源列表,不包括协议和端口

于 2018-01-23T16:18:34.253 回答
0

正如 Miklós 所提到的,其中一个站点的 url 应该与 localhost 不同。我做了一个快速测试,它在没有 https 的情况下工作,所以步骤如下:

  1. 转到 IIS 管理器并创建一个新站点。将您的 sensenet 安装的 web 文件夹的路径添加为物理路径,并添加 localhost 以外的主机名。

在此处输入图像描述

  1. 将您的自定义主机名添加到您的主机文件 (\Windows\System32\drivers\etc\hosts)

  2. 打开 sensenet 的管理界面(又名内容资源管理器)并将这个新 url 添加到 Default_Site 的 urllist(如果您编辑站点内容,您可以修改列表)

在此处输入图像描述

通过上面的这些步骤,我设法修复了 CORS 错误。

于 2018-01-24T15:21:06.813 回答
0

我相信您的请求与标头或方法(POST、GET、PUT 等)不匹配

以你的方式尝试这个或类似的东西。
[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]

于 2018-01-24T08:30:44.057 回答