Ejemplo n.º 1
0
        /// <summary>
        /// Sends registration token for the given username
        /// </summary>
        /// <param name="user">User to send the registration token</param>
        public void SendRegistrationToken(User user)
        {
            UserSecurityCode confirmationToken = null;

            do
            {
                confirmationToken = UserSecurityCode.CreateSecurityCode(user, "Registration");
            } while (this.FindUserForRegistrationToken(confirmationToken.Code) != null);

            user.RegistrationConfirmationToken = confirmationToken.EncryptedCode;
            UserManagerExtensions.Update(this, user);

            TextParser     parser = new TextParser(this.manager);
            TextDefinition td     = parser.ParseMessage("RegistrationEmail", new Dictionary <Model.Messages.ReplaceableObjectKeys, object>()
            {
                { ReplaceableObjectKeys.User, user },
                { ReplaceableObjectKeys.Code, confirmationToken.Code }
            });
            IdentityResult result = new IdentityResult();

            try
            {
                SmtpMailClient.SendMail(user.Email, "OPSMC RePLAY Registration", td.Text, td.Html);
            }
            catch (Exception ex)
            {
                // TODO add logger audit
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public void Dispose_CanBeCalledMultipleTimes()
        {
            SmtpMailClient client = new SmtpMailClient();

            client.Dispose();
            client.Dispose();
        }
        public void Dispose_MultipleTimes()
        {
            SmtpMailClient client = new SmtpMailClient("", 587, "", "");

            client.Dispose();
            client.Dispose();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sends a forgotten password token by email to the user for resetting the password
        /// </summary>
        /// <param name="username">The name of the user</param>
        public void ForgottenPassword(string username)
        {
            User u = null;

            try
            {
                u = this.FindByName(username);
                if (u == null)
                {
                    throw this.manager.MessageHandler.GetError(ErrorCodes.USER_UNKNOWN);
                }
                string         token  = this.GeneratePasswordResetToken(u.Id); // HttpUtility.UrlEncode(MachineKeyEncryption.Encrypt(u.Id + "|" + this.GeneratePasswordResetToken(u.Id)));
                TextParser     parser = new TextParser(this.manager);
                TextDefinition start  = parser.ParseMessage("NotificationStart", new Dictionary <ReplaceableObjectKeys, object>()
                {
                    { ReplaceableObjectKeys.User, u }, { ReplaceableObjectKeys.Code, token }
                });
                TextDefinition end = parser.ParseMessage("NotificationEnd", null);
                TextDefinition td  = parser.ParseMessage("PasswordReset", new Dictionary <ReplaceableObjectKeys, object>()
                {
                    { ReplaceableObjectKeys.User, u }, { ReplaceableObjectKeys.Code, token }
                });
                string text = start.Text + td.Text + end.Text;
                string html = start.Html + td.Html + end.Html;
                SmtpMailClient.SendMail(u.Email, "Password reset token", text, html);

                Logger.Audit(new Audit(Actions.FORGOT_PASSWORD, AuditEventType.READ, u));
            }
            catch (Exception ex)
            {
                Logger.Audit(new Audit(Actions.FORGOT_PASSWORD, AuditEventType.READ, typeof(User), "UserName", username, false, ex.Message));
                throw ex;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sends an email message asynchronously
        /// Gets the subject and body from the MessageHandler as Text Definitions. The definitions requested are based upon the message.Subject and message.Body respectively
        /// </summary>
        /// <param name="message">The message to send</param>
        /// <returns>The async message</returns>
        public Task SendAsync(IdentityMessage message)
        {
            AccessHandlerManager manager    = new AccessHandlerManager();
            TextDefinition       subject    = manager.MessageHandler.GetTextDefinitionByCode(message.Subject);
            TextDefinition       bodyFormat = manager.MessageHandler.GetTextDefinitionByCode(message.Body);

            SmtpMailClient.SendMail(message.Destination, subject == null ? message.Subject : subject.Text, bodyFormat == null ? message.Body : bodyFormat.Text, bodyFormat == null ? message.Body : bodyFormat.Html);

            return(Task.FromResult(0));
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            //普通测试
            SmtpMailClient client = new SmtpMailClient("smtp.163.com", 25, true, "XXX", "XXX");

            client.Timeout = 18000;
            MailMessage msg = new MailMessage();

            msg.From = new MailAccount("*****@*****.**", "janky");
            msg.AddTo(new MailAccount("*****@*****.**", "测试1"));
            msg.AddTo(new MailAccount("*****@*****.**"));
            msg.AddCC(new MailAccount("*****@*****.**"));
            //msg.AddCC(new MailAccount("*****@*****.**"));
            msg.MailContent = "<p>IHU 发信IP因发送垃圾邮件或存在异常的连接行为,被暂时挂起。请检测发信IP在历史上的发信情况和发信程序是否存在异常;</p><a href='http://163.com'>163 邮箱</a>";
            msg.Subject     = "测试";
            msg.IsHtml      = true;
            client.Send(msg);

            //附件测试
            client         = new SmtpMailClient("smtp.163.com", 25, true, "XXX", "XXX");
            client.Timeout = 18000;
            msg            = new MailMessage();
            msg.From       = new MailAccount("*****@*****.**", "janky");
            msg.AddTo(new MailAccount("*****@*****.**", "测试1"));
            msg.AddTo(new MailAccount("*****@*****.**"));
            msg.AddCC(new MailAccount("*****@*****.**"));
            msg.MailContent = "<img src='cid:test1234' /><p>IHU 发信IP因发送垃圾邮件或存在异常的连接行为,被暂时挂起。请检测发信IP在历史上的发信情况和发信程序是否存在异常;</p><a href='http://163.com'>163 邮箱</a>";
            msg.Subject     = "测试";
            msg.IsHtml      = true;

            msg.AddAttachment(@"c:\test\test.txt");
            msg.AddAttachment(@"c:\test\taiwan.jpg", "test1234"); //这里是content-id
            msg.AddAttachment(@"c:\test\filechktool.rar");
            client.SendAsync(msg);

            //ssl 测试
            SmtpServerInfo info = new SmtpServerInfo("smtp.gmail.com", 465, true, "XXX", "janky,.XXX");

            info.EnableSsl = true;
            client         = new SmtpMailClient(info);
            client.Timeout = 18000;
            msg            = new MailMessage();
            msg.From       = new MailAccount("*****@*****.**", "系统");
            msg.AddTo(new MailAccount("*****@*****.**", "测试1"));
            msg.AddTo(new MailAccount("*****@*****.**"));
            msg.AddCC(new MailAccount("*****@*****.**"));
            msg.MailContent = "<p>IHU 发信IP因发送垃圾邮件或存在异常的连接行为,被暂时挂起。请检测发信IP在历史上的发信情况和发信程序是否存在异常;</p><a href='http://163.com'>163 邮箱</a>";
            msg.Subject     = "测试";
            msg.IsHtml      = true;
            client.Send(msg);
            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public ActionResult ContactForm(FormCollection frm)
        {
            ContactMailViewModel model = new ContactMailViewModel {
                Name    = frm["name"],
                Email   = frm["email"],
                Phone   = frm["phone"],
                Comment = frm["comment"]
            };
            SmtpMailClient mail    = new SmtpMailClient();
            string         toEmail = ConfigurationManager.AppSettings["AdminEmail"];
            string         content = this.RenderRazorView("Mails/Contact", model);

            mail.PostMail(toEmail, "Iletisim Formu", content);
            return(View());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Notifies the given user of the token
        /// </summary>
        /// <param name="token">The token to send</param>
        /// <param name="manager">The manger that requested it</param>
        /// <param name="user">The user to send the token to</param>
        /// <returns>A task that is sending the token</returns>
        public override Task NotifyAsync(string token, Microsoft.AspNet.Identity.UserManager <Model.Users.User, string> manager, Model.Users.User user)
        {
            TextParser     parser  = new TextParser(this.Manager);
            TextDefinition subject = parser.ParseMessage(this.Subject, new Dictionary <ReplaceableObjectKeys, object>()
            {
                { ReplaceableObjectKeys.Code, token }, { ReplaceableObjectKeys.User, user }
            });
            TextDefinition body = parser.ParseMessage(this.BodyFormat, new Dictionary <ReplaceableObjectKeys, object>()
            {
                { ReplaceableObjectKeys.Code, token }, { ReplaceableObjectKeys.User, user }
            });

            new TaskFactory().StartNew(() => { SmtpMailClient.SendMail(user.Email, subject.Text, body.Text, body.Html); });

            return(Task.FromResult <int>(0));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates or updates a patient.
        /// If a user with the given Username doesn't exist it will be created, if it does exist, the patient will be added to that user
        /// </summary>
        /// <param name="externalId">The external ID of the patient</param>
        /// <param name="userName">The username of the user</param>
        /// <param name="email">The email</param>
        /// <param name="title">the title of the patient</param>
        /// <param name="firstName">The first name</param>
        /// <param name="lastName">The last name</param>
        /// <param name="dateOfBirth">The date of birth</param>
        /// <param name="mobilePhone">The patients mobile phone</param>
        /// <returns>The created or updated Patient</returns>
        public Patient CreateOrUpdatePatient(string externalId, string userName, string email, string title, string firstName, string lastName, DateTime dateOfBirth, string mobilePhone)
        {
            try
            {
                SecuritySession.Current.VerifyAccess(Actions.CREATE_OR_UPDATE_PATIENT);
                if (userName.Length > 450)
                {
                    throw this.manager.MessageHandler.GetError(ErrorCodes.USERNAME_LENGTH_EXCEEDED);
                }
                if (userName.Contains("\\") || userName.Contains("/"))
                {
                    throw this.manager.MessageHandler.GetError(ErrorCodes.USERNAME_CONTAINS_ILLEGAL_CHARACTERS);
                }
            }
            catch (Exception ex)
            {
                Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(Patient), "Email", email, false, ex.Message));
                throw ex;
            }

            IdentityResult result   = new IdentityResult(null);
            User           existing = this.Users.Where(u => u.UserName == userName).SingleOrDefault();
            User           user     = null;

            if (existing == null)
            {
                try
                {
                    result = UserManagerExtensions.Create(this, new User()
                    {
                        UserName = userName, Email = email, PhoneNumber = mobilePhone, Title = title, FirstName = firstName, LastName = lastName
                    });
                    if (result.Succeeded)
                    {
                        user = this.Users.Where(u => u.UserName == userName).SingleOrDefault();
                    }
                    else
                    {
                        throw new PCHIError(ErrorCodes.GENERAL_IDENTITY_RESULT_ERROR, result.Errors.Aggregate((s1, s2) => { return(s1 + "\n" + s2); }));
                    }
                    Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, user));
                }
                catch (Exception ex)
                {
                    Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(User), "UserName", userName, false, ex.Message));
                    throw ex;
                }
            }
            else
            {
                user = existing;
            }

            Patient patient    = null;
            Patient newPatient = null;

            if (user != null)
            {
                patient = !string.IsNullOrWhiteSpace(externalId) ? this.manager.UserAccessHandler.GetPatientByExternalId(externalId) : null;
                try
                {
                    if (patient != null)
                    {
                        Patient p = patient;
                        p.Title     = title;
                        p.FirstName = firstName;
                        p.LastName  = lastName;
                        p.ProxyUserPatientMap.Add(new ProxyUserPatientMap(user, p));
                        p.DateOfBirth = dateOfBirth;
                        p.Email       = email;
                        p.PhoneNumber = mobilePhone;
                        p.ExternalId  = externalId;
                        this.manager.UserAccessHandler.Update(p);
                        Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.MODIFIED, p));
                    }
                    else
                    {
                        Patient p = new Patient();
                        p.Title     = title;
                        p.FirstName = firstName;
                        p.LastName  = lastName;
                        p.ProxyUserPatientMap.Add(new ProxyUserPatientMap(user, p));
                        p.DateOfBirth = dateOfBirth;
                        p.Email       = email;
                        p.PhoneNumber = mobilePhone;
                        p.ExternalId  = externalId;
                        this.manager.UserAccessHandler.Add(p);
                        newPatient = p;
                        Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, p));
                    }

                    this.AddToRole(user.Id, "PatientProxy");
                }
                catch (Exception ex)
                {
                    Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(Patient), "Email", email, false, ex.Message));
                    throw ex;
                }
            }

            // Only send the registration mail if the user is created (i.e. existing is null)
            if (existing == null && user != null)
            {
                UserSecurityCode confirmationToken = null;
                do
                {
                    confirmationToken = UserSecurityCode.CreateSecurityCode(user, "Registration");
                } while (this.FindUserForRegistrationToken(confirmationToken.Code) != null);

                user.RegistrationConfirmationToken = confirmationToken.EncryptedCode;
                UserManagerExtensions.Update(this, user);

                // string confirmationToken = HttpUtility.UrlEncode(MachineKeyEncryption.Encrypt(user.UserName));
                TextParser     parser = new TextParser(this.manager);
                TextDefinition td     = parser.ParseMessage("RegistrationEmail", new Dictionary <Model.Messages.ReplaceableObjectKeys, object>()
                {
                    { ReplaceableObjectKeys.Patient, newPatient },
                    { ReplaceableObjectKeys.Code, confirmationToken.Code }
                });

                SmtpMailClient.SendMail(user.Email, "OPSMC RePLAY Registration", td.Text, td.Html);
            }

            if (newPatient != null)
            {
                try
                {
                    QuestionnaireUserResponseGroup group = this.manager.QuestionnaireAccessHandler.CreateQuestionnaireUserResponseGroup(newPatient.Id, BusinessLogic.Properties.Settings.Default.NewRegistrationQuestionnaire, null, null);
                    Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, group));
                }
                catch (Exception ex)
                {
                    Logger.Audit(new Audit(Actions.CREATE_OR_UPDATE_PATIENT, AuditEventType.ADD, typeof(QuestionnaireUserResponseGroup), "Id", null, false, ex.Message));
                    throw ex;
                }
            }

            return(newPatient == null ? patient : newPatient);
        }
Ejemplo n.º 10
0
 public void SetUp()
 {
     client = new SmtpMailClient();
 }