2017-02-28 95 views
1

根据该文件,我相信这是为了让CORS唯一需要的行:ServiceStack的JavaScript /打字稿客户端和CORS

Plugins.Add(new CorsFeature());

从不同的网站

然后:

var client = new JsonServiceClient('https://my-app.azurewebsites.net'); 
client.get(new something()); 

错误返回的是:

对预检请求的响应未通过访问控制l检查:响应 中'Access-Control-Allow-Credentials'标头的 值为'',当请求的凭证模式为 'include'时,该值必须为'true'。

没有身份验证,我正在使用所有的默认设置,这里缺少JsonServiceClient在设置中调用其他服务器的内容吗?

回答

3

问题最终发生在JsonServiceClient上。我必须将凭据设置为省略。

client.credentials="omit"

4

CorsFeature的默认值没有启用允许凭证。尝试将服务器更改为以下内容。

Plugins.Add(new CorsFeature(allowCredentials: true)); 

如果您尝试击中需要它们发送的端点,客户端将需要包含凭据。

编辑: 鉴于(我认为)你正在尝试从另一个域上git验证端点,这里是我用来做同样的事情的一个例子。域名已被更改。

Plugins.Add(new CorsFeature(
    allowCredentials: true, 
    allowedHeaders: "Content-Type, Allow, Authorization, Origin", 
    allowOriginWhitelist: new[] 
    { 
     "https://example.com", 
     "https://api.example.com", 
     "https://www.example.com", 
     "http://dev.example.com" 
    } 
)); 
+0

仍然不起作用:'对预检请求的响应未通过访问控制检查:响应中的'Access-Control-Allow-Origin'标头的值在请求的凭证模式下不能为通配符'*'是'包括'。 Origin'https:/my-app2.azurewebsites.net'因此是不允许访问的.' – lucuma

+0

我已经更新了答案,将从另一个白名单域中完整地显示出工作身份验证示例。 –

+0

我也尝试过。我想出了这个问题,它不是后端的CORS设置,而是前端的JsonServiceClient。我不得不设置'client.credentials =“omit”'。 – lucuma

2

我配置的服务如上所述:

Plugins.Add(new CorsFeature(
     allowCredentials: true, 
     allowedMethods: "GET, POST, PUT, DELETE, OPTIONS", 
     allowedHeaders: "Content-Type, Allow, Authorization, Origin", 
     allowOriginWhitelist: new[] 
      { 
        "http://localhost:4200", 
        "http://localhost:63342", 
        "http://localhost:63342", 
        "http://localhost:3000", 
        "http://my.site.com" 
      })); 

和我有2个功能登录()和GetContacts()

class AppComponent { 
*** 
     constructor(){ 
     this.client = new JsonServiceClient("http://my.site.com"); 
     this.client.credentials = "omit"; 
     } 

     async Login(){ 

     var auth = new wbs.Authenticate(); 
     auth.UserName = this.username; 
     auth.Password = this.password; 

     var authResponse = await this.client.post(auth); 

     console.log(authResponse); 
     } 

     async GetContacts(){ 
     try { 
      this.contacts = await this.client.post("Contacts_Get"); 
      console.log(this.contacts); 
     } catch(e) { 
      this.contacts = []; 
      console.log("Failed to get:", e.responseStatus); 
     } 
     } 
} 

“servicestack客户端”:“ 0.0.30“,

我依次调用这些函数:

1. Login() 
2. ContactsGet() 

登录运行良好,但在Internet Explorere和Safari ContactsGet失败时,它返回状态401,但在Chrome中运行。

帮忙请在我的错误? 谢谢!

enter image description here

enter image description here

enter image description here

UPDATE

IIS设置

enter image description here

+0

@Layoric请参阅这篇文章。 –

+0

默认情况下,请升级到[servicestack-client](https://github.com/ServiceStack/servicestack-client)的最新版本v0.0.36,其中包含Cookies。 – mythz

+0

@mythz我更新客户端到最新版本,如果我本地运行IIS快速工程,但是当我部署到IIS服务器不起作用。请参阅更新中的问题。 –