【问题标题】:Bearer tokens container /manual validation不记名令牌容器/手动验证
【发布时间】:2019-07-08 18:38:45
【问题描述】:

由于不记名令牌正在验证传入的令牌,它应该有一些容器或至少为此提供方法。

那么有什么简单的方法可以获取所有生成令牌的服务器列表/数组吗?或者也许是一些接受字符串标记并返回布尔结果的公共方法?

我需要:

-获取每天生成的所有令牌的数量

-检查令牌是否存在于列表中,它的状态是什么

【问题讨论】:

  • 默认没有获取你想要的数据的方法,你需要在生成的时候手动存储token并检查它的状态。
  • 我知道如何实现客户令牌验证并将令牌保存在数据库中。在我的情况下,有一个 api 使用默认的承载验证,我只需要 1-2 方法和自定义验证,而不需要在所有 api 中改变行为
  • 是的,你想要多少方法都没关系,但这里的重点是没有提供这些数据的默认方法,所以你需要自定义它。

标签: asp.net asp.net-core asp.net-core-2.0 asp.net-core-webapi bearer-token


【解决方案1】:

如果你想手动验证令牌,你可以试试这个

private bool ValidateVideoToken(string token)
        {
            var symmetricKey = Encoding.UTF8.GetBytes("symmetricKey");
            var securityKey = new SymmetricSecurityKey(symmetricKey);
            var encryptKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("encryptKey"));//16char
            var jwt = new JwtSecurityToken(token);

            // Verification
            var tokenValidationParameters = new TokenValidationParameters()
            {
                ClockSkew = TimeSpan.Zero,
                ValidAudiences = new string[]
                {
                      "Audience"
                },
                ValidIssuers = new string[]
                {
                      "Issure"
                },
                IssuerSigningKey = securityKey,
                TokenDecryptionKey = encryptKey,
                ValidIssuer = "Issure"
            };

            var handler = new JwtSecurityTokenHandler();

           var claimsPrincipal = handler.ValidateToken(token, tokenValidationParameters, out SecurityToken validatedToken);
            if (claimsPrincipal?.Claims == null || !claimsPrincipal.Claims.Any())
            {
                return false;
            }
            return true;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2018-07-27
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多