diff --git a/FunGame.Server/Controllers/Authenticator.cs b/FunGame.Server/Controllers/Authenticator.cs
index 671e43c..9e63982 100644
--- a/FunGame.Server/Controllers/Authenticator.cs
+++ b/FunGame.Server/Controllers/Authenticator.cs
@@ -1,9 +1,79 @@
using Milimoe.FunGame.Core.Api.Transmittal;
+using Milimoe.FunGame.Core.Api.Utility;
+using Milimoe.FunGame.Core.Library.Constant;
+using Milimoe.FunGame.Core.Library.SQLScript.Entity;
+using Milimoe.FunGame.Server.Model;
+using Milimoe.FunGame.Server.Others;
+using Milimoe.FunGame.Server.Utility;
+using TFA = Milimoe.FunGame.Server.Utility.TFA;
namespace Milimoe.FunGame.Server.Controllers
{
public class Authenticator : Core.Library.Common.Architecture.Authenticator
{
- public Authenticator(SQLHelper SQLHelper) : base(SQLHelper) { }
+ public TFA Login2FA = new();
+
+ private readonly ServerModel Server;
+ private readonly SQLHelper SQLHelper;
+ private readonly MailSender? MailSender;
+
+ public Authenticator(ServerModel Server, SQLHelper SQLHelper, MailSender? MailSender) : base(SQLHelper)
+ {
+ this.Server = Server;
+ this.SQLHelper = SQLHelper;
+ this.MailSender = MailSender;
+ }
+
+ public override bool AfterAuthenticator(AuthenticationType type, params object[] args)
+ {
+ if (type == AuthenticationType.Username)
+ {
+ // 添加2FA二次验证等
+ string username = (string)args[0];
+ string code = Login2FA.GetTFACode(username);
+ if (MailSender != null)
+ {
+ // 获取此账号的邮箱
+ string email = "";
+ SQLHelper.ExecuteDataSet(UserQuery.Select_IsExistUsername(username));
+ if (SQLHelper.Success && SQLHelper.DataSet.Tables[0].Rows.Count > 0)
+ {
+ email = Convert.ToString(SQLHelper.DataSet.Tables[0].Rows[0][UserQuery.Column_Email]) ?? "";
+ }
+ // 发送验证码
+ if (email != "")
+ {
+ string ServerName = Config.ServerName;
+ string Subject = $"[{ServerName}] FunGame 双重认证";
+ string Body = $"亲爱的 {username},
您正在登录[{ServerName}],为了保证安全性,需要进行邮箱验证,您的验证码是 {code} ,10分钟内有效,请及时输入!
{ServerName}
{DateTimeUtility.GetDateTimeToString(TimeType.DateOnly)}";
+ string[] To = new string[] { email };
+ if (MailSender.Send(MailSender.CreateMail(Subject, Body, System.Net.Mail.MailPriority.Normal, true, To)) == MailSendResult.Success)
+ {
+ ServerHelper.WriteLine(Server.GetClientName() + $" 已向{email}发送验证码:{code}");
+ }
+ else
+ {
+ ServerHelper.WriteLine(Server.GetClientName() + " 无法发送验证码");
+ ServerHelper.WriteLine(MailSender.ErrorMsg);
+ }
+ }
+ else
+ {
+ ServerHelper.WriteLine(Server.GetClientName() + $" 验证码为:{code},请服务器管理员告知此用户");
+ }
+ }
+ else // 不使用MailSender的情况
+ {
+ ServerHelper.WriteLine(Server.GetClientName() + $" 验证码为:{code},请服务器管理员告知此用户");
+ }
+ }
+ return true;
+ }
+
+ public override bool BeforeAuthenticator(AuthenticationType type, params object[] args)
+ {
+ // 添加人机验证或频繁验证等
+ return true;
+ }
}
}
diff --git a/FunGame.Server/Controllers/DataRequestController.cs b/FunGame.Server/Controllers/DataRequestController.cs
index b210c4f..727668c 100644
--- a/FunGame.Server/Controllers/DataRequestController.cs
+++ b/FunGame.Server/Controllers/DataRequestController.cs
@@ -29,7 +29,7 @@ namespace Milimoe.FunGame.Server.Controller
public DataRequestController(ServerModel server)
{
Server = server;
- Authenticator = new(SQLHelper);
+ Authenticator = new(Server, SQLHelper, MailSender);
}
public Hashtable GetResultData(DataRequestType type, Hashtable data)
diff --git a/FunGame.Server/Utilities/TFA.cs b/FunGame.Server/Utilities/TFA.cs
new file mode 100644
index 0000000..d639c86
--- /dev/null
+++ b/FunGame.Server/Utilities/TFA.cs
@@ -0,0 +1,12 @@
+using Milimoe.FunGame.Server.Model;
+
+namespace Milimoe.FunGame.Server.Utility
+{
+ public class TFA : Core.Api.Utility.TFA
+ {
+ public override bool IsAvailable(string username)
+ {
+ return true;
+ }
+ }
+}