This commit is contained in:
2025-08-07 09:16:23 +08:00
parent c97bd0ab55
commit 70bfe43a80
34 changed files with 532 additions and 135 deletions

View File

@@ -11,5 +11,6 @@ public class AccountDestroySystem : DestroySystem<Account>
self.Password = null;
self.CreateTime = 0;
self.LoginTime = 0;
self.Region = 0;
}
}

View File

@@ -40,13 +40,13 @@ internal static class AuthenticationComponentSystem
Log.Info($"鉴权服务器启动成功Position:{self.Position} AuthenticationCount:{self.AuthenticationCount}");
}
internal static async FTask<(uint ErrorCode, long AccountId)> Login(this AuthenticationComponent self,
internal static async FTask<(uint ErrorCode, long AccountId, int region)> Login(this AuthenticationComponent self,
string userName, string password)
{
// 1、检查传递的参数是否完整
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
{
return (ErrorCode.ErrArgs, 0);
return (ErrorCode.ErrArgs, 0, 0);
}
// 检查账号是否应该在当前鉴权服务器中处理
@@ -55,7 +55,7 @@ internal static class AuthenticationComponentSystem
{
// 这个3代表的是当前账号不应该在这个鉴权服务器处理。
// return (3, 0);
return (ErrorCode.ErrServer, 0);
return (ErrorCode.ErrServer, 0, 0);
}
var scene = self.Scene;
@@ -90,10 +90,10 @@ internal static class AuthenticationComponentSystem
if (account == null)
{
return (ErrorCode.ErrAccountOrPass, 0);
return (ErrorCode.ErrAccountOrPass, 0, 0);
}
return (ErrorCode.Successful, account.Id);
return (ErrorCode.Successful, account.Id, account.Region);
}
uint result = 0;
@@ -103,7 +103,7 @@ internal static class AuthenticationComponentSystem
if (account == null)
{
// 没有注册
return (ErrorCode.ErrAccountOrPass, -1); //返回-1用于判断是否需要自动注册
return (ErrorCode.ErrAccountOrPass, -1, 0); //返回-1用于判断是否需要自动注册
}
if (account.Password != password)
@@ -126,10 +126,10 @@ internal static class AuthenticationComponentSystem
if (result != 0)
{
return (result, 0);
return (result, 0, 0);
}
return (ErrorCode.Successful, account.Id);
return (ErrorCode.Successful, account.Id, account.Region);
}
}
@@ -138,14 +138,14 @@ internal static class AuthenticationComponentSystem
/// </summary>
/// <param name="self"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="region"></param>
/// <param name="source"></param>
internal static async FTask<uint> Register(this AuthenticationComponent self, string username, string password,
internal static async FTask<uint> Register(this AuthenticationComponent self, string username, int region,
string source)
{
// 1、检查传递的参数是否完整
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
if (string.IsNullOrEmpty(username))
{
// 这个1代表的是参数不完整。
return ErrorCode.ErrArgs;
@@ -187,7 +187,8 @@ internal static class AuthenticationComponentSystem
//3、执行到这里的话表示数据库或缓存没有该账号的注册信息需要咱们创建一个。
account = Entity.Create<Account>(scene, true, true);
account.Username = username;
account.Password = password;
account.Password = username;
account.Region = region;
account.CreateTime = TimeHelper.Now;
// 写入这个实体到数据中
await worldDateBase.Save(account);

View File

@@ -12,7 +12,8 @@ public static class AuthenticationHelper
/// <param name="userName">用户名</param>
/// <param name="password">用户密码</param>
/// <returns></returns>
public static async FTask<(uint ErrorCode, long AccountId)> Login(Scene scene, string userName, string password)
public static async FTask<(uint ErrorCode, long AccountId, int region)> Login(Scene scene, string userName,
string password)
{
return await scene.GetComponent<AuthenticationComponent>().Login(userName, password);
}
@@ -22,12 +23,12 @@ public static class AuthenticationHelper
/// </summary>
/// <param name="scene"></param>
/// <param name="username">用户名</param>
/// <param name="password">用户密码</param>
/// <param name="region">注册地区</param>
/// <param name="source">注册的来源/原因</param>
/// <returns></returns>
public static async FTask<uint> Register(Scene scene, string username, string password, string source)
public static async FTask<uint> Register(Scene scene, string username, int region, string source)
{
return await scene.GetComponent<AuthenticationComponent>().Register(username, password, source);
return await scene.GetComponent<AuthenticationComponent>().Register(username, region, source);
}
/// <summary>

View File

@@ -29,24 +29,25 @@ public static class AuthenticationJwtComponentSystem
ValidateIssuer = true, // 验证发行者
ValidateAudience = true, // 验证受众
ValidateIssuerSigningKey = true, // 验证签名密钥
ValidIssuer = "Fantasy", // 有效的发行者
ValidAudience = "Fantasy", // 有效的受众
ValidIssuer = "NoBug", // 有效的发行者
ValidAudience = "NoBug", // 有效的受众
IssuerSigningKey = new RsaSecurityKey(rsa) // RSA公钥作为签名密钥
};
}
public static string GetToken(this AuthenticationJwtComponent self, long aId, string address, uint sceneId)
public static string GetToken(this AuthenticationJwtComponent self, long aId, string address, uint sceneId, int region)
{
var jwtPayload = new JwtPayload()
{
{ "aId", aId },
{ "Address", address },
{ "SceneId", sceneId }
{ "SceneId", sceneId },
{ "Region", region },
};
var jwtSecurityToken = new JwtSecurityToken(
issuer: "Fantasy",
audience: "Fantasy",
issuer: "NoBug",
audience: "NoBug",
claims: jwtPayload.Claims,
expires: DateTime.UtcNow.AddMilliseconds(3000),
signingCredentials: self.SigningCredentials);

View File

@@ -11,9 +11,10 @@ public static class AuthenticationJwtHelper
/// <param name="aId">AccountId</param>
/// <param name="address">目标服务器的地址</param>
/// <param name="sceneId">分配的Scene的Id</param>
/// <param name="region">账号所属地区</param>
/// <returns></returns>
public static string GetToken(Scene scene, long aId, string address, uint sceneId)
public static string GetToken(Scene scene, long aId, string address, uint sceneId, int region)
{
return scene.GetComponent<AuthenticationJwtComponent>().GetToken(aId, address, sceneId);
return scene.GetComponent<AuthenticationJwtComponent>().GetToken(aId, address, sceneId, region);
}
}