OOPAndPOP/AuthSample/AuthToken.cs

(完整代码) 1KB

using Common.Extension;
using Common.Security;

namespace DesignModeConsole.OOPAndPOP.AuthSample
{
    /// <summary>
    /// 授权token
    /// </summary>
    public class AuthToken
    {
        /*
         需求描述
         把 URL、AppID、密码、时间戳拼接为一个字符串;
        对字符串通过加密算法加密生成 token;
        根据时间戳判断 token 是否过期失效;
        验证两个 token 是否匹配。
         */

        public AuthToken(string token, long createTome)
        {
            Token = token;
            CreateTime = createTome;
        }

        public AuthToken(string token, long createTime, long expiredTimeInterval) : this(token, createTime)
        {
            ExpiredTimeInterval = expiredTimeInterval;
        }

        private static readonly long _defaule_Expired_Time_Interval = 1 * 60 * 1000;

        private string Token { get; }

        public long CreateTime { get; }

        public long ExpiredTimeInterval { get; } = _defaule_Expired_Time_Interval;

        public static AuthToken Create(string baseUrl, string appid, string password, long timestamp)
        {
            var token = string.Concat(baseUrl + "&" + appid + "&" + password + "&" + timestamp).GetMd5Hash();
            return new AuthToken(token, timestamp);
        }

        public bool IdExpired()
        {
            var timestamp = DateTime.Now.GetTimestamp(true);//秒时间戳
            return (timestamp - CreateTime) > 60;//限制token有效期60s
        }

        public bool Match(AuthToken authToken)
        {
            return authToken.Token == Token;
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。