예제 #1
0
 public SignUpController(ApplicationContext context,
                         EmailMessageSender emailSender, SimpleLogger logger, AccountRegistrationService registrationService, DomainNameService domainNameService)
 {
     this.context             = context;
     this.emailSender         = emailSender;
     this.logger              = logger;
     this.registrationService = registrationService;
     this.domainNameService   = domainNameService;
 }
 public PasswordResetController(ApplicationContext context,
                                EmailMessageSender emailSender,
                                SimpleLogger logger, DomainNameService domainNameService)
 {
     this.context           = context;
     this.emailSender       = emailSender;
     this.logger            = logger;
     this.domainNameService = domainNameService;
 }
        public EmailMessagerViewModel()
        {
            emailSenderManager = new EmailMessageSender();
            messager           = new Messager();

            Groups = emailSenderManager.GetGroups();
            if (Groups.Count > 0)
            {
                SelectedGroup = Groups[0];
            }
        }
예제 #4
0
        private static void BridgeDesignPattern()
        {
            IMessageSender textMessageSender = new TextMessageSender();
            Message        textMessage       = new TextMessage(textMessageSender);

            textMessage.Send();

            IMessageSender emailMessageSender = new EmailMessageSender();
            Message        emailMessage       = new EmailMessage(emailMessageSender);

            emailMessage.Send();
        }
예제 #5
0
        public IActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                bool thereIsNoSuchEmailYet = context.EmailLoginInfo
                                             .FirstOrDefault(a => a.Email == model.Email) == null;

                if (thereIsNoSuchEmailYet)
                {
                    //Отправка сообщения на указанный email, чтобы удостовериться, что он принадлежит этому пользователю
                    if (EmailMessageSender.EmailIsValid(model.Email))
                    {
                        Guid guid = Guid.NewGuid();
                        var  link = GetEmailConfirmationLink(model, guid);

                        bool sendIsOk = emailSender.SendEmailCheck(model.Email, model.Name, link);

                        if (sendIsOk)
                        {
                            var tmpAccount = new TemporaryAccountWithUsernameAndPassword()
                            {
                                Name             = model.Name,
                                Password         = model.Password,
                                Email            = model.Email,
                                RegistrationDate = DateTime.UtcNow,
                                Guid             = guid
                            };

                            context.TemporaryAccountWithUsernameAndPassword.Add(tmpAccount);
                            context.SaveChanges();
                        }
                    }
                    return(RedirectToAction("MyBots", "MyBots"));
                }
                else
                {
                    ModelState.AddModelError("", "Аккаунт с таким email уже существует");
                }
            }
            return(View(model));
        }
예제 #6
0
 public TestController(IMessageSender sender, EmailMessageSender emailMessage, MessageSender messageSender)
 {
     _message       = sender.Send();
     _emailMessage  = emailMessage.Send();
     _messageSender = messageSender.Send();
 }
 public void Setup()
 {
     this.target = new EmailMessageSender();
 }
        public IActionResult PasswordResetEnterEmail(string email)
        {
            if (!EmailMessageSender.EmailIsValid(email))
            {
                ModelState.AddModelError("", "Введённый email неверный");
                return(View());
            }

            Account account = context.EmailLoginInfo
                              .Include(info => info.Account)
                              .SingleOrDefault(_acc => _acc.Email == email)?.Account;

            if (account != null)
            {
                //Отправка сообщения

                Guid guid = Guid.NewGuid();

                AccountToResetPassword tmpRecordDb = new AccountToResetPassword()
                {
                    AccountId = account.Id, GuidPasswordSentToEmail = guid
                };

                //TODO Перезаписывать запрос на смену пароля если уже он уже есть
                List <AccountToResetPassword> recordsWithTheSameAccountId = context.AccountsToResetPassword
                                                                            .Where(_tmpRecord => _tmpRecord.AccountId == account.Id)
                                                                            .ToList();

                if (!recordsWithTheSameAccountId.Any())
                {
                    context.AccountsToResetPassword.Add(tmpRecordDb);
                }
                else if (recordsWithTheSameAccountId.Count() == 1)
                {
                    recordsWithTheSameAccountId[0].GuidPasswordSentToEmail = guid;
                }
                else
                {
                    throw new Exception("В базе не должно быть больше одной записи для смены пароля");
                }

                string domain = domainNameService.GetDomainName();
                var    link   = $"https://{domain}/PasswordReset/PasswordResetOnlyNewPass?guid={guid.ToString()}&accountId={account.Id}";


                bool sendIsOk = emailSender.SendPasswordReset(email, account.Name, link);


                if (!sendIsOk)
                {
                    //если email не отправился, то удалить из БД запись о возможности сброса пароля
                    context.AccountsToResetPassword.Remove(tmpRecordDb);
                }
                context.SaveChanges();

                string message = "На вашу почту отправлено письмо. Для того, чтобы сбросить пароль следуйте инструкциям в письме. ";
                return(RedirectToAction("Success", "StaticMessage", new { message }));
            }
            else
            {
                ModelState.AddModelError("", "Аккаунта с таким email не существует.");
            }

            return(View());
        }
예제 #9
0
        public void Test_message()
        {
            string longM  = "this is a very very very long message...";
            string shortM = "short SMS";

            IMessageSender smsMessageSender   = new SMSMessageSender();
            IMessageSender emailMessageSender = new EmailMessageSender();

            // Short message
            ShortMessage shortMessage = new ShortMessage(smsMessageSender);
            {
                shortMessage.SendMessage(shortM);
                Assert.That(shortMessage.GetMessageSentCount(), Is.EqualTo(1));

                shortMessage.SendMessage(shortM);
                shortMessage.SendMessage(shortM);
                shortMessage.SendMessage(shortM);
                shortMessage.SendMessage(shortM);

                Assert.That(shortMessage.GetMessageSentCount(), Is.EqualTo(5));

                Assert.That(shortMessage.GetSenderType(), Is.EqualTo(SenderType.SMS));
                Assert.That(shortMessage.GetSenderType(), !Is.EqualTo(SenderType.Email));
            }

            // Null message
            {
                var ex = Assert.Throws <ArgumentNullException>(() => { shortMessage.SendMessage(string.Empty); });
                Assert.That(ex.Message, Is.EqualTo("Invalid message. (Parameter 'message')"));
            }

            // Long message
            {
                var ex = Assert.Throws <ArgumentException>(() => { shortMessage.SendMessage(longM); });
                Assert.That(ex.Message, Is.EqualTo("Invalid message: message is too long. (Parameter 'message')"));
            }

            // Long message
            LongMessage longMessage = new LongMessage(emailMessageSender);
            {
                longMessage.SendMessage(longM);
                Assert.That(longMessage.GetMessageSentCount(), Is.EqualTo(1));

                longMessage.SendMessage(longM);
                longMessage.SendMessage(longM);
                Assert.That(longMessage.GetMessageSentCount(), Is.EqualTo(3));

                Assert.That(longMessage.GetSenderType(), !Is.EqualTo(SenderType.SMS));
                Assert.That(longMessage.GetSenderType(), Is.EqualTo(SenderType.Email));
            }

            // Null message
            {
                var ex = Assert.Throws <ArgumentNullException>(() => { longMessage.SendMessage(string.Empty); });
                Assert.That(ex.Message, Is.EqualTo("Invalid message. (Parameter 'message')"));
            }

            // Short message
            {
                var ex = Assert.Throws <ArgumentException>(() => { longMessage.SendMessage(shortM); });
                Assert.That(ex.Message, Is.EqualTo("Invalid message: message is too short. (Parameter 'message')"));
            }
        }