2016-12-01 3 views
0

1つのクッキー値を、それ自身の長さでいくつかに分割したいと思います。例:4095は私が知る限度であるので、6000の長さのクッキーをそれぞれ2000の長さの3つのクッキーに分けたいと思う。MVCでクッキーを分割する

サンプル:

Cookie to be divide: 
    Name : "Foo"; 
    Value.Length: 6000 bytes; 

Cookies to be written: 
    (1) Name: "Foo"; 
    Value.Length: 2000 bytes, 
    (2) Name: "Foo1"; 
    Value.Length: 2000 bytes, 
    (3) Name: "Foo2"; 
    Value.Length: 2000 bytes, 

は.NETでそれを行うにはどのような方法(C#の - MVC5)はありますか? 私はそれをサポートしているのですか、それとも手動で実装する必要があるのですか?

+0

6kクッキー?あなたはそれをしたいと思いますか?彼らは*すべての*リクエストをホストに送ってきます。それは深刻なオーバーヘッドになるでしょう。なぜあなたはクッキーに多くの情報を詰め込む必要がありますか? – spender

+0

DBから一度だけユーザー設定(サイトテーマ、ユーザー指定の左メニューなど)をプルしたいと思っています。その後、クライアント側からそれらを読み込みます。フェデレーテッド認証はこれを独自に行います。そのため、いくつかサポートが必要だと思いました。 – Hasan

+2

そのものはクッキーに属していません。 [LocalStorage](https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage)に保存するか、DBから再度取得して、通常のクッキー内の識別子。ブラウザからサーバに**リクエストごとに6kを追加することは、本当に悪い考えです***。 – spender

答えて

0

私の質問に対する回答は見つかりませんでした。消費者が言うように、クッキーを使用するつもりはないが、1つのネットワークパッケージは最大1024バイトで構成されている。

したがって、クッキーは私が実装しようとしていることを行う良い方法ではありません。だから私は代わりにmemキャッシングを使い始める。

しかし、私がここで質問したように誰かがsthを検索した場合、ここにコードがあります:コントローラの拡張子としてのCookieヘルパー。

public static class CookieHelper 
{ 
    public static IEnumerable<HttpCookie> SplitCookieValue(this IController con, string cookieName, string cookieValue, int maxValueLength = 394) 
    { 
     #region Local variables 

     var lengthOfCookieValue = cookieValue.Length; 
     List<HttpCookie> resultCookieList = new List<HttpCookie>(); 
     var tempValue = cookieValue.Trim(); 
     var tempDictionary = new Dictionary<string, string>(); 

     if (maxValueLength > cookieName.Length) { maxValueLength = maxValueLength - cookieName.Length; } 

     #endregion 

     if (maxValueLength > 4094) 
     { 
      throw new Exception($"Max value:{maxValueLength} already overflows 4094 bytes!"); 
     } 

     #region Constructing temporary cookie dictionary 
     if (lengthOfCookieValue > maxValueLength) 
     { 
      var charCount = 1; 
      var dictionaryCounter = -1; 
      do 
      { 
       dictionaryCounter++; 
       try 
       { 
        tempDictionary.Add(dictionaryCounter.ToString(), tempValue.Substring(0, maxValueLength)); 
        charCount += maxValueLength; 

        tempValue = cookieValue.Substring(charCount - 1, cookieValue.Length - charCount); 
       } 
       catch (Exception) 
       { 
        break; 
       } 
      } while (charCount < lengthOfCookieValue); 
      if (!string.IsNullOrEmpty(tempValue)) 
      { 
       tempDictionary.Add((dictionaryCounter).ToString(), tempValue.Substring(0, tempValue.Length)); 
      } 
     } 
     else 
     { 
      tempDictionary.Add("", cookieValue); 
     } 

     #endregion 

     #region Constructing results from temporary cookie dictionary 
     if (tempDictionary != null) 
     { 
      foreach (var item in tempDictionary) 
      { 
       if (item.Key == "0") 
       { 
        resultCookieList.Add(new HttpCookie($"{cookieName}", $"{item.Value}")); 
       } 
       else 
       { 
        resultCookieList.Add(new HttpCookie($"{cookieName}{item.Key}", $"{item.Value}")); 
       } 
      } 
     } 
     #endregion 

     return resultCookieList; 

    } 

    public static string GetCookiesValue(this IController con, HttpResponseBase p, string cookieName) 
    { 
     string resultValue = null; 
     var resultCookieKeys = p.Cookies.AllKeys.Where(cookiekey => cookiekey.Contains(cookieName)); 
     if (resultCookieKeys.FirstOrDefault() != null) 
     { 
      resultValue = string.Empty; 
      foreach (var cookieKey in resultCookieKeys) 
      { 
       resultValue += p.Cookies[cookieKey].Value; 
      } 
     } 

     return resultValue; 

    } 

    public static void AddOrOverWriteCookie(this IController con, HttpResponseBase p, string cookieName, string cookieValue, int maxValueSize = 394) 
    { 
     var allCookiesWhichContainsKey = p.Cookies.AllKeys.Where(cookiekey => cookiekey.Contains(cookieName)); 

     if (allCookiesWhichContainsKey.FirstOrDefault() != null) 
     { 
      foreach (var cookieKey in allCookiesWhichContainsKey) 
      { 
       p.Cookies.Remove(cookieKey); 
      } 
     } 

     var cookiesToBeWrite = SplitCookieValue(con, cookieName, cookieValue, maxValueSize); 

     if (cookiesToBeWrite.Count() > 180) 
     { 
      // Most of the browsers set their limit to 180 per domain! 
      throw new Exception($"Too many cookies! Cookies Count: {cookiesToBeWrite.Count()}{Environment.NewLine}Hint: Up the MaxValueSize..."); 
     } 

     foreach (var cookie in cookiesToBeWrite) 
     { 
      p.Cookies.Add(cookie); 
     } 
    } 
} 
関連する問題