/// <inheritdoc/>
        public async Task <TokenContainerModel> CreateAsync(string username, string password, CancellationToken ct)
        {
            Ensure.ArgumentNotNull(username, nameof(username));
            Ensure.ArgumentNotNull(password, nameof(password));
            Ensure.ArgumentNotNull(ct, nameof(ct));

            ct.ThrowIfCancellationRequested();

            var user = await userManager.FindByNameAsync(username);

            if (user == null)
            {
                throw new UnexpectedNullException("Incorrect username or password.");
            }

            bool isValid = await userManager.CheckPasswordAsync(user, password);

            if (!isValid)
            {
                throw new UnexpectedNullException("Incorrect username or password.");
            }

            var result = await CreateContainerAsync(UserModel.Create(user, constants), ct);

            var command = new CreateRefreshTokenCommand()
            {
                Token  = result.RefreshTokenEncoded,
                UserId = user.Id.ToString()
            };

            await mediator.Send(command, ct);

            return(result);
        }
        protected override void Given(TestContext <IMessageDispatcher> context)
        {
            _refreshToken = Guid.NewGuid().ToString("n");
            _clientId     = Guid.NewGuid().ToString("n");
            var userId = Guid.NewGuid().ToString("n");

            var command = new CreateRefreshTokenCommand(_refreshToken, _clientId, userId, "me", "*", DateTimeOffset.Now,
                                                        DateTimeOffset.Now);

            var result = context.Service.ExecuteNowWithTimeout(command);

            result.Succeeded.ShouldBeTrue(result.ToString);
        }
        protected override void Given(TestContext <IMessageDispatcher> context)
        {
            var command = new CreateRefreshTokenCommand(
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                DateTimeOffset.Now,
                DateTimeOffset.Now);

            var result = context.Service.ExecuteNowWithTimeout(command);

            result.Succeeded.ShouldBeTrue(result.ToString());
        }
        private async Task StoreRefreshToken(SerializeRefreshTokenContext context)
        {
            var principal  = context.Ticket.Principal;
            var properties = context.Ticket.Properties;

            var command = new CreateRefreshTokenCommand(
                context.Ticket.GetTicketId(),
                context.Request.ClientId,
                principal.GetClaim(ClaimTypes.NameIdentifier),
                principal.GetClaim(ClaimTypes.Name),
                context.HttpContext.Connection.RemoteIpAddress?.ToString(),
                properties.IssuedUtc.GetValueOrDefault(),
                properties.ExpiresUtc.GetValueOrDefault());

            var result = await ExecuteMessage(context, command);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException("Could not store the refreshtoken");
            }
        }