Commit b9db4d4f authored by liudong1993's avatar liudong1993

1

parent bcdf81a9
using EduSpider.Utility.Enum;
using System;
namespace EduSpider.Utility.API
{
/// <summary>
/// API请求token携带的用户信息
/// </summary>
public class TokenUserInfo
{
/// <summary>
/// 用户ID
/// </summary>
public string uid { get; set; }
/// <summary>
/// 请求消息
/// </summary>
public ApiRequestFromEnum requestFrom { get; set; }
/// <summary>
/// 集团编号
/// </summary>
public int groupId { get; set; }
}
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace EduSpider.Utility
{
/// <summary>
/// DES 加解密
/// </summary>
public class DES
{
private static readonly byte[] _webapikey = { 0xF1, 0x12, 0xA3, 0xD1, 0xBA, 0x54, 0x2A, 0x88 };
private static readonly byte[] _webapiiv = { 0x04, 0xAE, 0x57, 0x83, 0x56, 0x28, 0x66, 0xA7 };
/// <summary>
/// DES加密(单向,只能C#用)
/// </summary>
/// <param name="EncryptString">加密字符串</param>
/// <returns></returns>
public static string Encrypt(string EncryptString)
{
return Encrypt(EncryptString, _webapikey, _webapiiv);
}
/// <summary>
/// DES加密(单向,只能C#用)
/// </summary>
/// <param name="EncryptString">待加密的字符串</param>
/// <param name="Key">加密密钥</param>
/// <param name="IV">初始化加密函数的变量</param>
/// <returns></returns>
private static string Encrypt(string EncryptString, byte[] Key, byte[] IV)
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(EncryptString);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="DecryptString">待解密的字符串</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decrypt(string DecryptString)
{
return Decrypt(DecryptString, _webapikey, _webapiiv);
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="DecryptString">待解密的字符串</param>
/// <param name="Key">解密密钥,要求为8位,和加密密钥相同</param>
/// <param name="IV">初始化加密函数的变量</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
private static string Decrypt(string DecryptString, byte[] Key, byte[] IV)
{
try
{
byte[] inputByteArray = Convert.FromBase64String(DecryptString);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return "";
}
}
}
}
\ No newline at end of file
......@@ -15,7 +15,6 @@ using System;
using System.Collections.Generic;
using JWT.Algorithms;
using JWT.Serializers;
using EduSpider.Utility.API;
namespace EduSpider.WebApi.Controllers
{
......@@ -58,7 +57,7 @@ namespace EduSpider.WebApi.Controllers
{
if (password != "Viitto!@#123")
{
password = DES.Encrypt(password);
password = DESHepler.Encrypt(password);
if (model.Password != password)
{
return ApiResult.Failed("密码错误", new { Error = 0 });
......@@ -76,28 +75,11 @@ namespace EduSpider.WebApi.Controllers
}
#region 获取进阶思维小程序端token
TokenUserInfo UserInfo = new() { uid = model.Id.ToString(), requestFrom = Utility.Enum.ApiRequestFromEnum.MiniProgram };
#region JWT
IDateTimeProvider provider = new UtcDateTimeProvider();
var now = provider.GetNow().AddMinutes(-1);
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch
var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);
var payload = new Dictionary<string, object>
{
{"iat",secondsSinceEpoch },
{"exp",secondsSinceEpoch + Config.JwtExpirTime },
{"jjsw_userInfo", UserInfo }
};
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
IJsonSerializer serializer = new JsonNetSerializer();
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
string secret = Config.JwtSecretKey;
string token = encoder.Encode(payload, secret);
#endregion
BaseUserInfo UserInfo = new() { BaseUserId = model.Id };
string token = JwtHelper.CreateToken(UserInfo, Config.JwtSecretKey, Config.JwtExpirTime);
#endregion
Model.Cache.UserInfo obj = new Model.Cache.UserInfo
Model.Cache.UserInfo obj = new()
{
Id = model.Id,
AccountType = model.AccountType,
......@@ -161,28 +143,11 @@ namespace EduSpider.WebApi.Controllers
}
#region 获取进阶思维小程序端token
TokenUserInfo UserInfo = new() { uid = model.Id.ToString(), requestFrom = Utility.Enum.ApiRequestFromEnum.MiniProgram };
#region JWT
IDateTimeProvider provider = new UtcDateTimeProvider();
var now = provider.GetNow().AddMinutes(-1);
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch
var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);
var payload = new Dictionary<string, object>
{
{"iat",secondsSinceEpoch },
{"exp",secondsSinceEpoch + Config.JwtExpirTime },
{"jjsw_userInfo", UserInfo }
};
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
IJsonSerializer serializer = new JsonNetSerializer();
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
string secret = Config.JwtSecretKey;
string token = encoder.Encode(payload, secret);
#endregion
BaseUserInfo UserInfo = new() { BaseUserId = model.Id };
string token = JwtHelper.CreateToken(UserInfo, Config.JwtSecretKey, Config.JwtExpirTime);
#endregion
Model.Cache.UserInfo obj = new Model.Cache.UserInfo
Model.Cache.UserInfo obj = new()
{
Id = model.Id,
AccountType = model.AccountType,
......
......@@ -46,6 +46,13 @@ namespace EduSpider.WebApi
options.Filters.Add<ApiFilterAttribute>();
});
//处理josn格式
services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(System.Text.Unicode.UnicodeRanges.All);
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
......@@ -89,7 +89,7 @@ namespace EduSpider.Spiders.ClassInRule
AccountId = item.StudId,
AccountType = Utility.Enum.AccountTypeEnum.Student,
OpenId = "",
Password = Utility.DES.Encrypt(item.StudentAccount[^6..]),
Password = DESHepler.Encrypt(item.StudentAccount[^6..]),
Status = 0,
UnionId = ""
});
......
......@@ -88,7 +88,7 @@ namespace EduSpider.Spiders.ClassInRule
AccountId = item.TeacherId,
AccountType = AccountTypeEnum.Teacher,
OpenId = "",
Password = Utility.DES.Encrypt(item.TeacherAccount[^6..]),
Password = DESHepler.Encrypt(item.TeacherAccount[^6..]),
Status = 0,
UnionId = ""
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment