Beispiel #1
0
        public void Deliver(int emailMessageId)
        {
            var entity = _entities.Query <EmailMessage>()
                         .EagerLoad(x => x.EmailAddress)
                         .ById(emailMessageId, false)
            ;

            // don't send the message if it has already been sent
            if (entity.SentOnUtc.HasValue)
            {
                return;
            }

            // don't sent the message if it is not supposed to be sent yet
            if (entity.SendOnUtc > DateTime.UtcNow)
            {
                return;
            }

            var from        = new MailAddress(entity.From);
            var to          = new MailAddress(entity.EmailAddress.Value);
            var mailMessage = new MailMessage(from, to)
            {
                Subject    = entity.Subject,
                Body       = entity.Body,
                IsBodyHtml = entity.IsBodyHtml,
            };

            var sendState = new SendEmailMessageState
            {
                EmailMessageId = emailMessageId,
            };

            _mail.Deliver(mailMessage, OnSendCompleted, sendState);
        }
        public async Task Handle(CreateEmailVerification command)
        {
            // find or create the email address
            var emailAddress = await _entities.Get <EmailAddress>().ByValueAsync(command.EmailAddress)
                               ?? new EmailAddress
            {
                Value       = command.EmailAddress.ToLower(), // TODO: allow users to change capitalization of email?
                HashedValue = await _queries.Execute(new HashedEmailValueBy(command.EmailAddress)),
            };

            // create random secret and ticket
            // note that changing the length of the secret requires updating the
            // email messages sent to the address, since those mention secret length
            var secret = _queries.Execute(new RandomSecret(10, 12));
            var ticket = _queries.Execute(new RandomSecret(20, 25));

            // make sure ticket is unique
            while (_entities.Query <EmailVerification>().ByTicket(ticket) != null)
            {
                ticket = _queries.Execute(new RandomSecret(20, 25));
            }

            // serialize a new user token to a string
            var token = await _userManager.UserTokenProvider.GenerateAsync(command.Purpose.ToString(), _userManager,
                                                                           new UserTicket
            {
                UserName = ticket,
            });

            // create the verification
            var verification = new EmailVerification
            {
                EmailAddress = emailAddress,
                Purpose      = command.Purpose,
                Secret       = secret,
                Ticket       = ticket,
                Token        = token,

                // change this, and you have to change the content of the email messages to reflect new expiration
                ExpiresOnUtc = DateTime.UtcNow.AddMinutes(30),
            };

            _entities.Create(verification);

            if (command.Commit)
            {
                await _entities.SaveChangesAsync();
            }

            command.CreatedEntity = verification;
        }
        public async Task Handle(RejectEmailVerification command)
        {
            // reject this email verification
            var verification = await _entities.Get <EmailVerification>()
                               .EagerLoad(x => x.EmailAddress)
                               .ByTicketAsync(command.Ticket, false);

            verification.Token = "Rejected";

            var email = verification.EmailAddress;

            email.UserId        = null;
            verification.Secret = null;
            var ticket = _queries.Execute(new RandomSecret(20, 25));

            // make sure ticket is unique
            while (_entities.Query <EmailVerification>().ByTicket(ticket) != null)
            {
                ticket = _queries.Execute(new RandomSecret(20, 25));
            }
            verification.Ticket = ticket;

            await _entities.SaveChangesAsync();
        }
 public Album GetAlbum(int albumId)
 {
     return(writeEntities.Query <Album>(x => x.Artist, x => x.Genre).Where(x => x.Id == albumId).FirstOrDefault());
 }