-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
218 lines (193 loc) · 7.34 KB
/
Copy pathApp.xaml.cs
File metadata and controls
218 lines (193 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using CredentialManagement; // 添加 CredentialManagement 命名空间
using FrpcUI.Class;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Media;
using System.Windows.Navigation;
namespace FrpcUI
{
public partial class App : Application
{
public static Process FrpcProcess { get; set; }
private const string CredentialTarget = "FrpcUI"; // 凭据唯一标识
private const string LoginStateFile = "loginState.json"; // 非敏感数据存储文件
private const int WindowWidth = 900;
private const int WindowHeight = 550;
public static Window MainWindowInstance { get; set; }
/// <summary>
/// 检查并删除过期的登录状态(基于凭据管理器)
/// </summary>
bool CheckIfCredentialExpired()
{
try
{
// 1. 检查凭据是否存在
using (var cred = new Credential { Target = "FrpcUI" })
{
if (!cred.Load()) return true;
}
// 2. 读取最后更新时间
using var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
if (!isoFile.FileExists("loginState.json")) return true;
using var stream = isoFile.OpenFile("loginState.json", FileMode.Open);
if (stream.Length == 0) return true;
using var reader = new StreamReader(stream);
string json = reader.ReadToEnd();
// 假设JSON结构包含LastUpdateTime字段
var data = JsonConvert.DeserializeObject<LoginState>(json);
if (data?.LastUpdateTime == null) return true;
// 3. 判断是否超过有效期
return (DateTime.Now - data.LastUpdateTime.Value).TotalDays > 5;
}
catch
{
return true;
}
}
class LoginState
{
public DateTime? LastUpdateTime { get; set; }
}
/// <summary>
/// 从凭据管理器和隔离存储加载完整登录状态
/// </summary>
public LoginModel LoadLoginState()
{
var loginModel = new LoginModel();
try
{
// 1. 从凭据管理器读取用户名和密码
using (var cred = new Credential())
{
cred.Target = CredentialTarget;
if (cred.Load())
{
loginModel.Token = cred.Password;
}
else
{
return null; // 没有找到凭据
}
}
// 2. 从隔离存储读取其他非敏感数据
IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
if (isoFile.FileExists("loginState.json"))
{
using (var stream = new IsolatedStorageFileStream("loginState.json", FileMode.Open, isoFile))
using (var reader = new StreamReader(stream))
{
string json = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<dynamic>(json);
loginModel.Msg = data.Msg;
loginModel.Mail = data.Mail;
loginModel.UserImg = data.UserImg;
loginModel.RealName = data.RealName;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"加载登录状态失败: {ex.Message}");
return null;
}
return loginModel;
}
/// <summary>
/// 删除保存的登录状态(凭据+非敏感数据)
/// </summary>
public bool DeleteLoginState()
{
try
{
bool deleted = false;
// 1. 删除凭据
using (var cred = new Credential())
{
cred.Target = CredentialTarget;
deleted = cred.Delete();
}
// 2. 删除非敏感数据文件
using var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
if (isoFile.FileExists(LoginStateFile))
{
isoFile.DeleteFile(LoginStateFile);
deleted = true;
}
return deleted;
}
catch (Exception ex)
{
Debug.WriteLine($"删除登录状态失败: {ex.Message}");
return false;
}
}
private NavigationWindow CreateLoginWindow()
{
// 获取默认 Window 样式
var defaultWindowStyle = (Style)Application.Current.Resources[typeof(Window)];
// 获取自定义样式
var hiddenNavigationStyle = (Style)FindResource("HiddenNavigationStyle");
// 创建合并后的样式
var mergedStyle = new Style(typeof(NavigationWindow), defaultWindowStyle);
// 将 HiddenNavigationStyle 的所有 Setter 添加到合并样式
if (hiddenNavigationStyle != null)
{
foreach (SetterBase setter in hiddenNavigationStyle.Setters)
{
mergedStyle.Setters.Add(setter);
}
}
return new NavigationWindow
{
Source = new Uri("Pages/LoginPage.xaml", UriKind.Relative),
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Background = Brushes.Transparent,
Width = WindowWidth,
Height = WindowHeight,
Style = mergedStyle
};
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
// 检查并删除过期登录状态
if (CheckIfCredentialExpired()) DeleteLoginState();
var savedLogin = LoadLoginState();
if (savedLogin?.Msg == "登录成功")
{
MainWindowInstance = new MainWindow();
}
else
{
MainWindowInstance = CreateLoginWindow();
}
MainWindowInstance.Show();
}
catch (Exception ex)
{
MessageBox.Show($"应用程序启动失败: {ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
Shutdown();
}
}
public void Window_Loaded(object sender, RoutedEventArgs e)
{
if (sender is Window window)
{
// 计算屏幕中心
var screenWidth = SystemParameters.PrimaryScreenWidth;
var screenHeight = SystemParameters.PrimaryScreenHeight;
window.Left = (screenWidth - window.ActualWidth) / 2;
window.Top = (screenHeight - window.ActualHeight) / 2;
}
}
}
}