示例#1
0
        public void SendEmail(string subject, string mailBody, string recipients, string ticketID, HttpPostedFileBase[] fileAttachments = null)
        {
            try
            {
                SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SmtpHost"], int.Parse(ConfigurationManager.AppSettings["SmtpPort"]));

                smtpClient.Credentials    = new NetworkCredential(ConfigurationManager.AppSettings["EmailSenderUserName"], ConfigurationManager.AppSettings["EmailSenderPassword"]);
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl      = bool.Parse(ConfigurationManager.AppSettings["SmtpSSL"]);
                MailMessage mail = new MailMessage();

                mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailSenderEmail"], ConfigurationManager.AppSettings["EmailSenderName"]);

                foreach (string email in recipients.Split(new char[] { ',' }))
                {
                    mail.To.Add(new MailAddress(email));
                }

                mailBody = mailBody +
                           "<p>Click <a href=\"" + SecurityHelper.BuildAbsolute("Home/TicketDetails/" + ticketID) + "\" title=\"View Ticket\">here</a></p>" +
                           "<p><strong>" + Session["displayName"] + "</strong><br>" +
                           "<strong>ICT HelpDesk</strong></p>" +
                           "<img src=\"" + SecurityHelper.BuildAbsolute("Content/assets/img/logo.png") + "\" alt=\"IEIA-Logo\" />";

                if (fileAttachments != null)
                {
                    if (fileAttachments.Length > 0)
                    {
                        string targetPath = HttpContext.Server.MapPath(this.fileAttachmentPath);

                        if (fileAttachments[0] != null)
                        {
                            List <TicketFile> ticketFiles = new List <TicketFile>();

                            foreach (var file in fileAttachments)
                            {
                                string fileName = Path.Combine(targetPath, file.FileName);
                                mail.Attachments.Add(new Attachment(fileName));
                            }
                        }
                    }
                }

                mail.Subject    = subject;
                mail.Body       = mailBody;
                mail.IsBodyHtml = true;

                smtpClient.Send(mail);
            }
            catch (Exception ex)
            {
                HomeController.LogError(ex, HttpContext.Server.MapPath("~/Error_Log.txt"));
            }
        }
示例#2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["ADIP"].ToString()))
            {
                bool isValid = pc.ValidateCredentials(model.UserName, model.Password);

                if (isValid)
                {
                    /*DirectoryEntry directoryEntry = new DirectoryEntry(ConfigurationManager.ConnectionStrings["ADConnectionString"].ConnectionString, model.UserName, model.Password);
                     * object nativeObject = directoryEntry.NativeObject;
                     * var userEmail = new DirectorySearcher(directoryEntry)
                     * {
                     *  Filter = "samaccountname=" + model.UserName,
                     *  PropertiesToLoad = { "mail" }
                     * }.FindOne().Properties["mail"][0];*/

                    string name = "", userEmail = "";
                    var    usr = UserPrincipal.FindByIdentity(pc, model.UserName);
                    if (usr != null)
                    {
                        name      = usr.DisplayName;
                        userEmail = usr.EmailAddress;
                    }

                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    Session["userEmail"]   = userEmail;
                    Session["displayName"] = name;


                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 &&
                        returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") &&
                        !returnUrl.StartsWith("/\\"))
                    {
                        if (!returnUrl.ToLower().Contains("/ticketdetails") && HomeController.IsMember(model.UserName, ConfigurationManager.AppSettings["ADAG"].ToString()))
                        {
                            returnUrl = SecurityHelper.BuildAbsolute("Home/Tickets");// returnUrl.Replace("/Home/Index", "/Home/Tickets");
                        }
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        if (HomeController.IsMember(model.UserName, ConfigurationManager.AppSettings["ADAG"].ToString()))
                        {
                            return(RedirectToAction("Tickets", "Home", new { status = "" }));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect");
                }
            }

            /*if (Membership.ValidateUser(model.UserName, model.Password))
             * {
             *  FormsAuthentication.SetAuthCookie(model.UserName, false);
             *  if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1
             *      && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//")
             *      && !returnUrl.StartsWith("/\\"))
             *  {
             *      return Redirect(returnUrl);
             *  }
             *  else
             *  {
             *      return RedirectToAction("Index", "Home");
             *  }
             * }
             * else
             * {
             *  ModelState.AddModelError("", "The user name or password provided is incorrect");
             * }*/

            return(View(model));
        }