【问题标题】:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost: XXXX is therefore not allowed access请求的资源上不存在“Access-Control-Allow-Origin”标头。来源 http://localhost: XXXX 因此不允许访问
【发布时间】:2014-06-16 02:39:29
【问题描述】:

我正在与 David Antramian 的 spa-authentication-example 合作。除了我使用 n 层方法并将我的数据/模型与 Web API 项目分开之外,我明确地遵循了他的示例。

当使用 Postman 并直接针对 API Web 应用程序时,一切正常:

但不是当我使用客户端 UI 时(我确实更改了 angularjs 的 user.js/salesdata.js 中的 url 以反映 2604 端口),不太好:

我试过“app.UseCors(CorsOptions.AllowAll);”在我的 Startup.Auth.cs 中无济于事。

在我的 API web.config 中也试过了,还是不行:

<httpProtocol>
    <customHeaders>
        <!-- Enable Cross Domain AJAX calls -->
        <remove name="Access-Control-Allow-Origin" />
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>

考虑可能是 Chrome 相关的安全问题,我也在 IE 中尝试过,但无济于事。

有什么想法吗?

这是我完整的post方法,和Postman是一样的:

this.authenticate = function(username, password, successCallback, errorCallback, persistData) {
  this.removeAuthentication();
  var config = {
    method: 'POST',
    url: 'http://localhost:2604/token',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: 'grant_type=password&username=' + username + '&password=' + password,
  };

【问题讨论】:

    标签: angularjs cors asp.net-identity


    【解决方案1】:

    根据教程:

    您应该能够使用 HTTP 请求工具(我的投票是 Postman)来调用 API。您需要做的是使用“grant_type=password&username=admin&password=password”形式的 x-www-form-urlencoded 数据对 localhost:{port}/token 进行 POST

    您没有在 http 请求中的任何位置设置 xss 防护数据“grant_type=password&username=admin&password=password”。

    因此,在您进行 HTTP POST 的代码中,正确发送正确参数的一种方法是将 xss 预防数据附加到 url。像这样:

    localhost:2604/token?grant_type=password&username=admin&password=password
    

    一定要设置username=(正确的用户名)&password=(正确的密码)

    就我个人而言,我也使用了 os_authType=basic 而不是 grant_type=password。 grant_type 是作者建议的,所以先试试吧。

    【讨论】:

    • 不起作用:this.authenticate = function(username, password, successCallback, errorCallback, persistData) { this.removeAuthentication(); var config = { 方法:'POST',网址:'localhost:2604/…',
    • 请发布您的完整 POST 方法。当您尝试附加 ?grant_type...(ect) 时,您是否得到完全相同的错误。
    • 完整发布方法见上文
    【解决方案2】:

    一怒之下,我执行了“更新包”,现在一切正常。也许我的各种应用程序中有相互冲突的 Newtonsoft 软件包。

    【讨论】:

      猜你喜欢
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      相关资源
      最近更新 更多