-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataProtectorOptions.cs
More file actions
83 lines (79 loc) · 2.79 KB
/
DataProtectorOptions.cs
File metadata and controls
83 lines (79 loc) · 2.79 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
/* ===============================================
* 功能描述:AspNetCore.DataProtection.DataProtectionOptions
* 创 建 者:WeiGe
* 创建日期:9/12/2018 11:02:57 PM
* ===============================================*/
using System;
using System.Collections.Generic;
using System.IO;
namespace AspNetCore.DataProtector
{
public class DataProtectorOptions
{
private const string DataProtectionKeysFolderName = "ASP.NET/DataProtection-Keys";
/// <summary>
/// Compress the data
/// </summary>
public bool UseCompress { get; set; }
/// <summary>
/// default 'false'
/// </summary>
public bool AutoGenerateKeys { get; set; } = false;
/// <summary>
/// default 30 days
/// </summary>
public TimeSpan KeyExpired { get; set; } = TimeSpan.FromDays(30);
static DirectoryInfo _keyPath;
internal DirectoryInfo KeyDirectory
{
get
{
if (_keyPath == null)
{
var userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
if (!string.IsNullOrEmpty(userProfile))
{
userProfile = Path.Combine(Environment.GetEnvironmentVariable("SystemDrive"), "Users",
Environment.GetEnvironmentVariable("APP_POOL_ID")?? Environment.GetEnvironmentVariable("UserName"),
"AppData\\Local");
}
List<string> keyPaths = new List<string>(5)
{
Environment.GetEnvironmentVariable("LOCALAPPDATA"),
userProfile,
Environment.GetEnvironmentVariable("HOME"),
AppContext.BaseDirectory
};
foreach (var path in keyPaths)
{
if (CreateDirectory(path, ref _keyPath))
{
break;
}
}
}
return _keyPath;
}
}
private static bool CreateDirectory(string basePath, ref DirectoryInfo _keyPath)
{
if (!string.IsNullOrEmpty(basePath))
{
try
{
var retVal = new DirectoryInfo(Path.Combine(basePath, DataProtectionKeysFolderName));
if (!retVal.Exists)
{
retVal.Create();
}
_keyPath = retVal;
return true;
}
catch
{
}
}
return false;
}
}
}