77namespace MultiCommentViewer
88{
99 using Microsoft . Web . WebView2 . Core ;
10- using Newtonsoft . Json ;
1110 // 必要な using
1211 using System ;
1312 using System . Collections . Generic ;
1413 using System . IO ;
15- using System . Net ;
16- using System . Security . Cryptography ;
1714 using System . Text ;
18- using System . Text . Json ;
1915 using System . Threading . Tasks ;
20- using System . Security ;
2116
2217 // Cookie DTO(保存フォーマット)
2318 public class CookieDto
@@ -32,23 +27,23 @@ public class CookieDto
3227 }
3328
3429 // ユーティリティ/保存ロジック
35- public static class CookieStorage
30+ public static class LocalCache
3631 {
3732 // exe フォルダの Cookies サブフォルダを返す
38- public static string GetCookieFolder ( )
33+ public static string GetCacheFolders ( )
3934 {
4035 var exeDir = Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . Location )
4136 ?? AppDomain . CurrentDomain . BaseDirectory ;
42- var folder = Path . Combine ( exeDir , "Cookies " ) ;
37+ var folder = Path . Combine ( exeDir , "LocalCache " ) ;
4338 Directory . CreateDirectory ( folder ) ;
4439 return folder ;
4540 }
4641
4742 // 保存先パス(サイト名で分ける)
48- public static string GetCookieFilePath ( string siteName )
43+ public static string GetCachePath ( string siteName )
4944 {
5045 var safeName = string . IsNullOrWhiteSpace ( siteName ) ? "default" : MakeFileSystemSafe ( siteName ) ;
51- return Path . Combine ( GetCookieFolder ( ) , safeName + ".bin" ) ;
46+ return Path . Combine ( GetCacheFolders ( ) , safeName + ".bin" ) ;
5247 }
5348
5449 // ファイル名に使える安全文字列に変換(簡易)
@@ -60,7 +55,7 @@ private static string MakeFileSystemSafe(string name)
6055 }
6156
6257 // CoreWebView2 の Cookie リストを DTO に変換して暗号化してファイルに保存
63- public static async Task SaveCookiesEncryptedAsync ( IEnumerable < CoreWebView2Cookie > cookies , string siteName )
58+ public static async Task WriteAsync ( IEnumerable < CoreWebView2Cookie > cookies , string siteName )
6459 {
6560 var list = new List < CookieDto > ( ) ;
6661 foreach ( var c in cookies )
@@ -97,50 +92,8 @@ public static async Task SaveCookiesEncryptedAsync(IEnumerable<CoreWebView2Cooki
9792 }
9893
9994 var json = System . Text . Json . JsonSerializer . Serialize ( list ) ;
100- var bytes = Encoding . UTF8 . GetBytes ( json ) ;
101-
102- var path = GetCookieFilePath ( siteName ) ;
103- File . WriteAllBytes ( path , bytes ) ;
95+ var path = GetCachePath ( siteName ) ;
96+ File . WriteAllText ( path , json , Encoding . UTF8 ) ;
10497 }
105-
106- // // 暗号化ファイルを復号して CookieContainer を返す
107- // public static CookieContainer LoadCookiesFromEncryptedFile(string siteName)
108- // {
109- // var path = GetCookieFilePath(siteName);
110- // if (!File.Exists(path)) return new CookieContainer();
111-
112- // var encrypted = File.ReadAllBytes(path);
113-
114- // // 復号
115- // var bytes = ProtectedData.Unprotect(encrypted, optionalEntropy: null, scope: DataProtectionScope.LocalMachine);
116- // var json = Encoding.UTF8.GetString(bytes);
117-
118- // var list = System.Text.Json.JsonSerializer.Deserialize<List<CookieDto>>(json);
119- // var container = new CookieContainer();
120-
121- // if (list != null)
122- // {
123- // foreach (var dto in list)
124- // {
125- // try
126- // {
127- // var cookie = new Cookie(dto.Name, dto.Value, dto.Path ?? "/", dto.Domain ?? "");
128- // if (dto.Expires.HasValue) cookie.Expires = dto.Expires.Value;
129- // cookie.HttpOnly = dto.IsHttpOnly;
130- // cookie.Secure = dto.IsSecure;
131-
132- // // CookieContainer.Add のドメイン取り扱いに注意:
133- // // domain は先頭にドットが必要な場合があるためそのまま渡す
134- // container.Add(cookie);
135- // }
136- // catch
137- // {
138- // // 無効な cookie があれば無視して続行
139- // }
140- // }
141- // }
142-
143- // return container;
144- // }
14598 }
146- }
99+ }
0 commit comments