private async Task<AuthorizationCode> GetAuthorizationCodeAsync(AuthorizationAttempt attempt)
 {
     if (attempt.Identity == null)
     {
         await UnitOfWork.GetRepository<AuthorizationAttempt>().LoadReferenceAsync(attempt, a => a.Identity);
     }
     if (attempt.Identity != null && attempt.Identity.Card == null)
     {
         await
             UnitOfWork.GetRepository<ClientIdentity>()
                 .LoadReferenceAsync(attempt.Identity, identity => identity.Card);
     }
     if (attempt.Identity != null && attempt.Identity.Card != null)
     {
         return
             await
                 UnitOfWork.GetRepository<AuthorizationCard>()
                     .GetCollectionQuery(attempt.Identity.Card, card => card.Codes)
                     .FirstOrDefaultAsync(code => code.Number == attempt.RequestedNumber);
     }
     return null;
 }
 private Task CloseAttemptAsync(AuthorizationAttempt lastAttempt)
 {
     lastAttempt.SuccessedAt = DateTime.UtcNow;
     UnitOfWork.GetRepository<AuthorizationAttempt>().Update(lastAttempt, attempt => attempt.SuccessedAt);
     return UnitOfWork.SaveAsync();
 }