示例#1
0
        public Token GetOrCreate(User user) {
            var existingToken = _tokenRepository.GetByUserId(user.Id).Documents.FirstOrDefault(t => t.ExpiresUtc > DateTime.UtcNow && t.Type == TokenType.Access);
            if (existingToken != null)
                return existingToken;

            var token = new Token {
                Id = StringExtensions.GetNewToken(),
                UserId = user.Id,
                CreatedUtc = DateTime.UtcNow,
                ModifiedUtc = DateTime.UtcNow,
                CreatedBy = user.Id,
                Type = TokenType.Access
            };
            _tokenRepository.Add(token);

            return token;
        }
 private void SetupTokenRequest(HttpRequestMessage request, Token token) {
     request.GetRequestContext().Principal = new ClaimsPrincipal(token.ToIdentity());
 }