首页 文章

CORS无法正常工作 - 401

提问于
浏览
1

尝试从WebApi获取数据 . 这是TypeScript中的服务调用:

GetStatus(hostName: string): any {
    this.Http.post(this.ServiceBaseAddress)
        .success(data => {
            return data;
        });
}

在服务器端,我已完成以下操作以启用CORS:

1:添加NugetPackage Microsoft.ASP.Net.Cors

2:将EnableCors属性添加到一个Controller(我正在尝试调用)

[EnableCors("*", "*", "*")]
public class DeployController : ApiController
{ //...
}

3:在WebApiConfig中启用CORS

public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.EnableCors(); //...

4:通过Web.config添加自定义标头(在system.webServer区域中)

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
  </customHeaders>
</httpProtocol>

我注意到Fiddler似乎做了一些非常讨厌的缓存,这让我困惑不止一次......现在,至少,我得到一个响应,其中包含以下 Headers :

HTTP/1.1 401 Unauthorized
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-SourceFiles: =?UTF-8?B?QzpcRGV2ZWxvcFxTUEFSQ1xEZXZlbG9wXFJ1bnRpbWVcU1BBUkMuTWFuYWdlbWVudEFwaVxNYW5hZ2VtZW50QXBpXGFwaVxkZXBsb3lcZ2V0aG9zdHM=?=
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Date: Mon, 30 Nov 2015 13:12:48 GMT
Content-Length: 6540
Proxy-Support: Session-Based-Authentication

响应的内容是服务的401错误站点 .

为了实现这一点,我尝试向WebApis Web.config添加匿名身份验证,但没有成功:

<authorization>
  <allow users="*"/>
</authorization>

任何想法我能做什么?我如何进行身份验证?


EDIT: 原来这确实是一个身份验证问题 . WebApi身份验证设置如下:

<authentication mode="Windows" />
  • 当我在IE或Chrome中调用Webservice URI时,我得到了预期的响应 .

  • 当我在Firefox中调用它并按取消而不是输入我的用户凭据时,我收到来自我网站的http调用的相同消息 - 401.2认证 Headers 的问题 .

话虽这么说,我需要知道如何告诉角度Ajax调用使用我的凭据 .

3 回答

  • 1

    HTTP / 1.1 401未经授权

    可能的情况

    • 未经身份验证,您不允许使用OPTIONS . 因此,在检查cors的OPTIONS查询中返回 401 .

    • CORS工作正常,但您的控制器还有一层您未在查询中发送的必需身份验证(您应该看到像 .aspxauth 或Basic Auth这样的cookie)

  • 0

    我有同样的问题,你应该添加 SupportsCredentials 选项 .

    asp.net 4

    configuration.EnableCors(new EnableCorsAttribute("https://mysite.mydomain.com", "*", "*") 
    {
            SupportsCredentials = true 
    });
    

    它看起来如何 asp.net 5

    private static void ConfigureCors(IServiceCollection services)
    {
        services.AddCors();
    
        var policy = new CorsPolicy();
        policy.Headers.Add("*");
        policy.Methods.Add("*");
        policy.Origins.Add("*");
        policy.SupportsCredentials = true;
        services.ConfigureCors(x => x.AddPolicy(DefaultCorsPolicy, policy));
    }
    
  • 1

    通过这里给出的所有答案以及一些研究,我发现了该做什么 - 首先,我必须修改角度ajax调用以传递我的凭证,如下所示:

    this.Http.get(this.ServiceBaseAddress + addressPart, { withCredentials: true });
    

    然后,如@michalczukm所示,我必须配置服务以接受这些凭据 .

    作为清理工作,我将所有与Cross-Origin相关的内容移至 web.config 并删除了属性

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:59664" />
        <add name="Access-Control-Allow-Credentials" value="true" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />        
      </customHeaders>
    </httpProtocol>
    

相关问题