10

我配置的Identity Server:如何設置客戶端cookie的到期日期?

public void Configuration(IAppBuilder app) 
{ 
    var factory = new IdentityServerServiceFactory().UseInMemoryClients(new Client[] { 
     new Client() 
     { 
      ClientName = "MyClient", 
      ClientId = "MyClientId", 
      Enabled = true, 
      Flow = Flows.Implicit, 
      RedirectUris = new List<string> { "MyClientServer/callback" }, 
     }; 
    }); 
} 

和客戶端服務器:

public void Configuration(IAppBuilder app) 
{ 
    var cookieOptions = new CookieAuthenticationOptions(); 
    cookieOptions.AuthenticationType = "Cookies"; 
    app.UseCookieAuthentication(cookieOptions); 

    var authenticationOptions = new OpenIdConnectAuthenticationOptions() { 
     Authority = "https://MyIdentityServer/core", 
     ClientId = "MyClientId", 
     SignInAsAuthenticationType = "Cookies", 
     UseTokenLifetime = true, 
     RedirectUri = "MyClientServer/callback" 
    }); 

    app.UseOpenIdConnectAuthentication(authenticationOptions); 
} 

當 「記住我」 選項身份餅乾已經過期日期用戶登錄:

idsvr.session expires 04 October ... 

但客戶端的cookie不:

.AspNet.Cookies at end of session 

我應該如何設置相同的到期日期到客戶端cookie?

UPDATE:

我可以設置客戶端應用程序的任何截止日期:

authenticationOptions.Provider = new CookieAuthenticationProvider() 
{ 
    OnResponseSignIn = (context) => 
    { 
     var isPersistent = context.Properties.IsPersistent; 
     if (isPersistent) // Always false 
     { 
      context.CookieOptions.Expires = DateTime.UtcNow.AddDays(30); 
     } 
    } 
}; 

但我不能確定何時設定截止日期。只有在用戶選擇「記住我」時才應該設置,但客戶端的IsPersistent選項始終爲false。

的問題存在於簡單的樣板項目過於: https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html

UPDATE2:

我需要,因爲漏洞在Safari中的客戶端的cookie是持久 - https://openradar.appspot.com/14408523

也許有些存在解決方法,所以我可以通過Identity to Client回傳過期日期?

UPDATE3:

其實,我們的身份和客戶端服務器有像app.server.localid.server.local相同的父域。也許我可以通過屬於父域的其他cookie(.server.local)傳遞過期日期?但我不知道可以在Identity上寫什麼,以及它可以在Client上應用的位置。

+1

問題尋求幫助調試(「**爲什麼不是這個代碼工作?**」)必須包括所期望的行爲,一*特定問題或錯誤,並在最短的代碼必要*重現它在**這個問題本身**。沒有**明確問題陳述**的問題對其他讀者沒有用處。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

回答

1

您需要處理cookie auth事件。開放標識中間件只是創建一個auth cookie,所以你可以從這些事件處理這個cookie的所有方面。您需要查看事件並嘗試一些小小的嘗試,以便能夠管理cookie的使用期限。

+0

如果用戶選中「記住我」,我可以在哪個事件中設置cookie過期日期? – Artem

2

IdentityServer發佈的cookie和客戶端應用程序發佈的cookie沒有任何關聯。 IdentityServer無法控制客戶端應用程序中的Cookie。

當您登錄到IdentityServer時,會發出一個cookie來跟蹤IdentityServer中經過身份驗證的用戶。這可以使用戶無需爲每個客戶端應用程序輸入憑據,便於單點登錄。

默認情況下,該cookie會持續用於該會話(因此一旦瀏覽器關閉就會過期),否則如果您設置「記住我」,它將在會話中持續一定的天數。

成功驗證來自IdentityServer的身份令牌後,客戶端應用程序中的cookie將發出。這個cookie可以有任何到期時間,任何政策,任何名稱。它完全由客戶端應用程序控制。在您的情況下,您的客戶端Cookie可以在客戶端應用程序中的CookieAuthenticationOptions中設置。

+0

是的,它完全由客戶端應用程序控制。但是我只需要在IdentityServer設置它時設置到期日期。如果IdentityServer側的IsPersistent標誌爲false,則客戶端應使用會話cookie。查看問題更新。 – Artem

+0

沒有機制可以做到這一點。這將不得不是一個自定義的實現,關閉規範。出於好奇,永久客戶端cookie需要什麼? –

+0

Safari中存在一個錯誤 - http://openradar.appspot.com/14408523,所以我需要一個持久性cookie作爲解決方法。 – Artem

1

您可以在java腳本中使用以下代碼在此處創建此cookie,以便在14天內過期。

var exdate = new Date(); 
exdate.setDate(exdate.getDate() + 14); 

document.cookie = "yourcookie=" + yourCookieValue + ";expires=" + exdate.toUTCString() + ";"; 
+1

出於安全原因,客戶端和身份Cookie僅出於http。 – Artem