2017-02-28 96 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服務器不起作用。請參閱更新中的問題。 –