IMemoryCache问题,无法转换为字符串?

时间:2017-04-04 09:07:09

标签: asp.net-core asp.net-core-mvc asp.net-core-1.1

当我在Postman中发送请求时,我无法输入语句if(ConvertedAccessTokenCache.Contains(accessToken)),我必须检查AccessTokenCache是​​否包含(accessToken)< - 授权:承载c2E6dGVsZWFkcmU1NSxudA == ...

但是当它检查它是否包含时,当鼠标悬停在该点上时,我得到“Microsoft.Extensions.Caching.Memory.MemoryCache”。

我缺少什么想法?

namespace ....Security.Token
    {
        public class TokenManager
        {
            private readonly IMemoryCache AccessTokenCache;
            private readonly IMemoryCache RefreshTokenCache;
            private CancellationTokenSource ctsAccess;
            private CancellationTokenSource ctsRefresh;

            private readonly uint RefreshTokenValidTime = 60 * 60 * 10; //seconds
    #if DEBUG
            private readonly uint TokenExpirationTime = 60 * 60 * 2; //seconds
    #else
            private readonly uint TokenExpirationTime = 60 * 5; //seconds
    #endif

            public TokenManager()
            {
                AccessTokenCache = new MemoryCache(new MemoryCacheOptions());
                RefreshTokenCache = new MemoryCache(new MemoryCacheOptions());
                ctsAccess = new CancellationTokenSource(60 * 60 * 2);
                ctsRefresh = new CancellationTokenSource(60 * 60 * 10);
            }

            public long ValidateToken(string accessToken)
            {
                var ConvertedAccessTokenCache = AccessTokenCache.ToString();

                if (ConvertedAccessTokenCache.Contains(accessToken))
                {
                    var token = (TokenResponse)AccessTokenCache.Get(accessToken);

                    var createdDate = GetCreatedDate(accessToken);
                    var validToDate = createdDate.AddSeconds(token.expires_in);

                    var clientId = GetClientId(accessToken);

                    if (validToDate > SystemTime.Now())
                    {
                        return clientId;
                    }
                }

                return -1L;
            }

        }
    }




public UserEntity GetUser(string credentials)
        {
            var token = new TokenManager();
            var clientId = token.ValidateToken(credentials);
            if (clientId == -1)
            {
                return null;
            }

            var user = Execute(context =>
            {
                var command = new GetCommand<UserEntity>(c => c.UserDataAccess.GetUser(clientId));
                return command.Execute(null, false, context);
            });

            return user;
        }

1 个答案:

答案 0 :(得分:0)

AccessTokenCache是MemoryCache的一个实例,当您致电ToString()时,请将其称为default implementation,它会返回完全限定名称。

您需要拨打TryGetValue或其中一种扩展方法,例如AccessTokenCache.Get<String>("key")

扩展方法可能需要声明。您需要先致电TryGetValueusing Microsoft.Extensions.Caching.Memory之类的扩展程序,然后才能使用它们。

public long ValidateToken(string accessToken)
{
    // Get<TokenResponse> or TryGetValue will return default value if not found
    // which is null in case of string and classes
    var token = AccessTokenCache.Get<TokenResponse>(accessToken);

    if (token != null)
    {
        var createdDate = GetCreatedDate(accessToken);
        var validToDate = createdDate.AddSeconds(token.expires_in);

        var clientId = GetClientId(accessToken);

        if (validToDate > SystemTime.Now())
        {
            return clientId;
        }
    }
}