iT邦幫忙

0

[MVC5]ADFS使用WsFederation中Cookie一小時延長問題

工作中遇到的情境,我們使用ADFS做認證,
實作方式是透過Microsoft.Owin.Security.WsFederation(ADFS端也設定了WsFederation),
同網路上眾多教學一樣,添加完下列內容,基本上就沒太大問題

            MetadataAddress = EpmsSettings.Default.AdfsMetadataAddress,
            Wtrealm = EpmsSettings.Default.AdfsWtrealm,

但最近遇到一個狀況是,.AspNet.Federation此Cookie每一小時就會到期需要重新取得進行更新,
這邊也確認過Cookie內容,其實到期時間是隨著瀏覽時間結束才結束,
所以推測一小時此時間是AdfsMetadataAddress或adfs其中一項預設設定。

上網尋求萬能google大神後亦有相同問題進行詢問
https://social.msdn.microsoft.com/Forums/vstudio/en-US/0091bcb2-c86d-4d36-a845-102ac24c4e93/default-token-lifetime-for-relying-party-trusts?forum=Geneva

初步找到方向進行修改,先嘗試過調整CookieAuthenticationOptions

new CookieAuthenticationOptions
{
      AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType,
      Provider = new CookieAuthenticationProvider
      {
            OnResponseSignIn = context =>
            {
                   context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
                   context.Properties.IsPersistent = true;
            }
      }
});

經過驗證發現並沒有生效,Cookie仍是一小時之後會到期並替換
後來發現必須在WsFederationAuthenticationOptions加入UseTokenLifetime設定

WsFederationAuthenticationOptions

      new WsFederationAuthenticationOptions
      {
             MetadataAddress = EpmsSettings.Default.AdfsMetadataAddress,
             Wtrealm = EpmsSettings.Default.AdfsWtrealm,
             UseTokenLifetime = false,
             ...

即可以順利延長Cookie時間,避免Ajax時會有因Cookie到期而錯誤的狀況出現

以上筆記


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言