初始化项目

This commit is contained in:
2025-11-20 11:03:29 +08:00
commit 86785bb77f
988 changed files with 325041 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
using Microsoft.AspNetCore.Mvc;
using OtpNet;
using System.Text;
using XSystem.Security.Cryptography;
namespace AUTS_Server.Service
{
public class EncryptionService : IEncryptionService
{
public string BuildTotpUri(string secret, string user, string issuer)
{
var issuerParameter = string.IsNullOrEmpty(issuer) ? "" : $"&issuer={Uri.EscapeDataString(issuer)}";
return $"otpauth://totp/{Uri.EscapeDataString(user)}?secret={secret}{issuerParameter}&algorithm=SHA1&digits=6&period=30";
}
public string Encrypt(string str)
{
MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] array = md5CryptoServiceProvider.ComputeHash(Encoding.Default.GetBytes(str));
StringBuilder stringBuilder = new StringBuilder();
foreach (byte b in array)
{
stringBuilder.Append(b.ToString("x2"));
}
return stringBuilder.ToString().ToUpper();
}
public GenerateKeyinfo GenerateKey()
{
GenerateKeyinfo generateKeyinfo = new GenerateKeyinfo();
var key = KeyGeneration.GenerateRandomKey(20);
string base32Secret = Base32Encoding.ToString(key);
var issuer = "AUTS";
var userAccount = "new.uts-data.com";
string totpSetupUrl = BuildTotpUri(base32Secret, userAccount, issuer);
generateKeyinfo.SecretKey = base32Secret;
generateKeyinfo.QrCodeSetupUrl = totpSetupUrl;
return generateKeyinfo;
// 返回密钥和二维码URL
//return Ok(new { SecretKey = base32Secret, QrCodeSetupUrl = totpSetupUrl });
}
public bool VerifyTOTP(VerifyTOTPRequest request)
{
var key = Base32Encoding.ToBytes(request.SecretKey);
long timeStepMatched;
var totp = new Totp(key);
var TotpCode = "";
bool isValid = totp.VerifyTotp(TotpCode, out timeStepMatched, new VerificationWindow(2, 2));
if (isValid)
{
// 验证成功
return true;
}
else
{
// 验证失;
return false;
}
}
}
public class GenerateKeyinfo
{
public string SecretKey { get; set; }
public string QrCodeSetupUrl { get; set; }
}
}

View File

@@ -0,0 +1,72 @@
using Microsoft.AspNetCore.Mvc;
using OtpNet;
using static AUTS_Server.Service.MultiFactorAuthController;
namespace AUTS_Server.Service
{
public interface IEncryptionService
{
//md5加密
string Encrypt(string str);
// 手动构建TOTP URI
string BuildTotpUri(string secret, string user, string issuer);
// 验证TOTP
bool VerifyTOTP(VerifyTOTPRequest request);
// 生成并返回密钥和二维码URL
GenerateKeyinfo GenerateKey();
}
[ApiController]
[Route("[controller]")]
public class MultiFactorAuthController : ControllerBase
{
// 生成并返回密钥和二维码URL
[HttpGet("GenerateKey")]
public IActionResult GenerateKey()
{
var key = KeyGeneration.GenerateRandomKey(20);
var base32Secret = Base32Encoding.ToString(key);
var issuer = "AUTS";
var userAccount = "new.uts-data.com";
var totpSetupUrl = BuildTotpUri(base32Secret, userAccount, issuer);
// 返回密钥和二维码URL
return Ok(new { SecretKey = base32Secret, QrCodeSetupUrl = totpSetupUrl });
}
// 验证TOTP
[HttpPost("VerifyTOTP")]
public IActionResult VerifyTOTP([FromBody] VerifyTOTPRequest request)
{
var key = Base32Encoding.ToBytes(request.SecretKey);
long timeStepMatched;
var totp = new Totp(key);
var TotpCode = "";
bool isValid = totp.VerifyTotp(TotpCode, out timeStepMatched, new VerificationWindow(2, 2));
if (isValid)
{
// 验证成功
return Ok(new { Success = true });
}
else
{
// 验证失败
return BadRequest(new { Success = false });
}
}
// 手动构建TOTP URI
private string BuildTotpUri(string secret, string user, string issuer)
{
var issuerParameter = string.IsNullOrEmpty(issuer) ? "" : $"&issuer={Uri.EscapeDataString(issuer)}";
return $"otpauth://totp/{Uri.EscapeDataString(user)}?secret={secret}{issuerParameter}&algorithm=SHA1&digits=6&period=30";
}
}
public class VerifyTOTPRequest
{
public string SecretKey { get; set; }
public string UserId { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace AUTS_Server.Service
{
public interface ILogHelperForService
{
void Init();
void WriteLine(string text);
}
}

View File

@@ -0,0 +1,7 @@
namespace AUTS_Server.Service
{
public interface IUserOperationLog
{
void UserLog(string Openration, string Device, string name);
}
}

View File

@@ -0,0 +1,38 @@
using System.Text;
namespace AUTS_Server.Service
{
public class LogHelperForService : ILogHelperForService
{
string LogFile = "";
public void Init()
{
string directory = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
DateTime curTime = DateTime.Now;
LogFile = directory + "\\" + curTime.ToString("yyyy-MM-dd") + ".txt";
if (!File.Exists(LogFile))
{
FileStream fs = File.Create(LogFile);
fs.Close();
}
}
public void WriteLine(string text)
{
lock (LogFile)
{
Init();
text += "\r\n";
using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
{
sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
}
}
}
}
}

View File

@@ -0,0 +1,156 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net;
using System.Text;
using uts_manage;
using XAct.Users;
namespace AUTS_Server.Service
{
public class UserOperationLog : IUserOperationLog
{
private readonly IHttpContextAccessor _httpContextAccessor;
public UserOperationLog(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void UserLog(string Openration, string Device,string name)
{
tbl_uts_useroperation tBL_UTS_UserOperation = new tbl_uts_useroperation();
string ip = string.Empty;
if (_httpContextAccessor.HttpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
{
ip = _httpContextAccessor.HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
}
else
{
var remoteIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress;
if (remoteIp != null)
{
if (remoteIp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ip = remoteIp.MapToIPv4().ToString();
}
else
{
ip = remoteIp.MapToIPv6().ToString();
}
}
}
string browserType = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();
string phone = _httpContextAccessor.HttpContext.Request.Cookies.TryGetValue("phone", out var phoneValue) ? phoneValue : null;
string PC = _httpContextAccessor.HttpContext.Request.Cookies.TryGetValue("PC", out var pcValue) ? pcValue : null;
tBL_UTS_UserOperation.UserName = name;
tBL_UTS_UserOperation.Database = "aa";
// Users.GerOnUserCustomer().DatabaseName;
tBL_UTS_UserOperation.Browser = browserType;//浏览器
tBL_UTS_UserOperation.Ip = ip;//ip
tBL_UTS_UserOperation.Device = Device;
tBL_UTS_UserOperation.CreationTime = DateTime.Now;//时间
tBL_UTS_UserOperation.Operation = Openration;
tBL_UTS_UserOperation.Location = GetBaiduIp(ip);
//SqlConnect.SqlInsertUserLog(tBL_UTS_UserOperation);
}
/// <summary>
/// 百度api
/// </summary>
/// <returns></returns>
public static string GetBaiduIp(string ip)
{
string location = "";
try
{
string url = $"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&co=&resource_id=6006&oe=utf8";
WebClient client = new WebClient();
var buffer = client.DownloadData(url);
string jsonText = Encoding.UTF8.GetString(buffer);
JObject jo = JObject.Parse(jsonText);
Root root = JsonConvert.DeserializeObject<Root>(jo.ToString());
foreach (var item in root.data)
{
location = item.location;
}
return location;
}
catch (Exception ex)
{
//Console.WriteLine(ex);
return location;
}
}
}
public class Root
{
public List<DataItem> data { get; set; }
}
public class DataItem
{
/// <summary>
///
/// </summary>
public string ExtendedLocation { get; set; }
/// <summary>
///
/// </summary>
public string OriginQuery { get; set; }
/// <summary>
///
/// </summary>
public string appinfo { get; set; }
/// <summary>
///
/// </summary>
public int disp_type { get; set; }
/// <summary>
///
/// </summary>
public string fetchkey { get; set; }
/// <summary>
/// 本地局域网
/// </summary>
public string location { get; set; }
/// <summary>
///
/// </summary>
public string origip { get; set; }
/// <summary>
///
/// </summary>
public string origipquery { get; set; }
/// <summary>
///
/// </summary>
public string resourceid { get; set; }
/// <summary>
///
/// </summary>
public int role_id { get; set; }
/// <summary>
///
/// </summary>
public int shareImage { get; set; }
/// <summary>
///
/// </summary>
public int showLikeShare { get; set; }
/// <summary>
///
/// </summary>
public string showlamp { get; set; }
/// <summary>
/// IP地址查询
/// </summary>
public string titlecont { get; set; }
/// <summary>
///
/// </summary>
public string tplt { get; set; }
}
}