36 lines
1.3 KiB
C#
36 lines
1.3 KiB
C#
using System;
|
||
using System.IdentityModel.Tokens.Jwt;
|
||
using Fantasy;
|
||
|
||
namespace NB.Gate;
|
||
|
||
public static class GateJWTHelper
|
||
{
|
||
/// <summary>
|
||
/// 验证令牌是否合法
|
||
/// </summary>
|
||
/// <param name="scene"></param>
|
||
/// <param name="token">ToKen</param>
|
||
/// <param name="accountId">如果验证成功会返回正常的AccountId</param>
|
||
/// <returns>如果是True表示验证成功,False表示验证失败</returns>
|
||
public static bool ValidateToken(Scene scene, string token, out long accountId)
|
||
{
|
||
if (!ValidateToken(scene, token, out JwtPayload payload))
|
||
{
|
||
// 如果令牌验证失败,表示当前令牌不合法、那就返回为false,让上层处理。
|
||
accountId = 0;
|
||
return false;
|
||
}
|
||
|
||
// 如果不等于当前Scene的ConfigId的话,把就表示该连接不应该连接到当前的Gate里。
|
||
// 所以理应把当前连接关闭掉。
|
||
var sceneId = Convert.ToInt64(payload["SceneId"]);
|
||
accountId = Convert.ToInt64(payload["aId"]);
|
||
return sceneId == scene.SceneConfigId;
|
||
}
|
||
|
||
private static bool ValidateToken(Scene scene, string token, out JwtPayload payload)
|
||
{
|
||
return scene.GetComponent<GateJWTComponent>().ValidateToken(token, out payload);
|
||
}
|
||
} |