47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
namespace Application.Domain;
|
|
|
|
public static class UserKeyTool
|
|
{
|
|
public static async Task<string> GetThickenData(string userId, params string[] data)
|
|
{
|
|
string key = await GetUserKey(userId);
|
|
return GetThickenDataByUserKey(key, data);
|
|
}
|
|
|
|
public static string GetThickenDataByUserKey(string key, params string[] data)
|
|
{
|
|
string _data = string.Join("", data);
|
|
return EncryptAssist.Md5Encryption($"{key}_{_data}").ToLower();
|
|
}
|
|
|
|
|
|
public static async Task<bool> CheckUserThickenData(string userId, string sign, params string[] data)
|
|
{
|
|
string key = await GetUserKey(userId);
|
|
string _sign = GetThickenDataByUserKey(key, data);
|
|
await ResetUserKey(userId);
|
|
return sign.ToLower() == _sign;
|
|
}
|
|
|
|
public static async Task<string> GetUserKey(string userId)
|
|
{
|
|
var key = string.Format(UserCache.BaseCacheKey, "UserKey");
|
|
var redis = App.GetService<IRedisCache>();
|
|
if (await redis.HExistsHashAsync(key, userId))
|
|
{
|
|
return await redis.GetHashAsync<string>(key, userId);
|
|
}
|
|
|
|
string data = StringAssist.RandomString(12);
|
|
await redis.AddHashAsync(key, userId, data);
|
|
return data;
|
|
}
|
|
|
|
private static async Task ResetUserKey(string userId)
|
|
{
|
|
string data = StringAssist.RandomString(12);
|
|
var key = string.Format(UserCache.BaseCacheKey, "UserKey");
|
|
var redis = App.GetService<IRedisCache>();
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
} |