Esempio n. 1
0
 public string Encrypt(string encryptString)
 {
     try
     {
         string EncryptionKey = options.Value.EncryptKey;
         byte[] clearBytes    = Encoding.Unicode.GetBytes(encryptString);
         using (Aes encryptor = Aes.Create())
         {
             Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, salt);
             encryptor.Key = pdb.GetBytes(32);
             encryptor.IV  = pdb.GetBytes(16);
             using (MemoryStream ms = new MemoryStream())
             {
                 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                 {
                     cs.Write(clearBytes, 0, clearBytes.Length);
                 }
                 encryptString = Convert.ToBase64String(ms.ToArray());
             }
         }
         return(encryptString);
     }
     catch (Exception e)
     {
         es.ThrowException("Encrypt error {0}", e.Message);
         return(null);
     }
 }
Esempio n. 2
0
        public void SendContactUsConfirmation(Message message2send)
        {
            if (message2send == null || message2send.MessageGroupId != MessageGroups.ContactUs)
            {
                es.ThrowException("No message to confirm contact us");
            }

            MimeMessage message = new MimeMessage();

            message.From.Add(new MailboxAddress("Links And News", op.Value.Emails.Support));
            message.To.Add(new MailboxAddress(message2send.AuthorName, message2send.AuthorEmail));
            message.Subject = message2send.Subject;


            BodyBuilder bodyBuilder = new BodyBuilder();

            bodyBuilder.HtmlBody = string.Format(
                @"
                    <p>Hello {0},</p>
                    <p>Thank you for contacting us!</p>
                    <p>Your message</p>
                    <p><q style='font-style: italic'>{1}</q></p>
                    <p>has been received.</p>
                    <p>We will respond as soon as possible.</p>
                    <br />
                    <p>Sincerely,</p>
                    <p>Links And News</p>
                ", message2send.AuthorName, message2send.MessageText);

            message.Body = bodyBuilder.ToMessageBody();

            send(message);
        }
Esempio n. 3
0
        public void PutLoginDataToCookies(LoginData loginData)
        {
            if (loginData == null || !loginData.Valid)
            {
                es.ThrowException("Wrong login data");
            }
            string json = JsonConvert.SerializeObject(loginData);
            string lp   = encryptionService.Encrypt(json);

            httpContext.HttpContext.Response.Cookies.Append("lp", lp,
                                                            new CookieOptions()
            {
                Path     = "/",
                HttpOnly = false,
                Secure   = false,
                Expires  = DateTimeOffset.MaxValue
            });
        }
Esempio n. 4
0
        public ContentCategory GetContentCategoryByName(string categoryName)
        {
            ContentCategory result = db
                                     .ContentCategories
                                     .AsNoTracking()
                                     .FromSql("spGetContentCategory {0}", categoryName)
                                     .FirstOrDefault();

            if (result == null)
            {
                log.LogWarning("Requested content category {0} was not found in db", categoryName);

                result = db.ContentCategories.AsNoTracking().FromSql("spGetContentCategory", "General").FirstOrDefault();
                if (result == null)
                {
                    es.ThrowException("Requested content category {0} was not found in db", ExceptionSeverity.Error, "General");
                }
            }
            return(result);
        }
Esempio n. 5
0
        // TODO: reduce login and page names down to reasonable minimum, because path length is limited wih 248 (?) characters
        public async Task <string> UploadElementImage(string login, PageElement pageElement, IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                es.ThrowException("File is empty");
            }

            if (string.IsNullOrWhiteSpace(options.Value.AccountImagesPath))
            {
                es.ThrowException("No account images path in configuration");
            }

            if (string.IsNullOrWhiteSpace(login))
            {
                es.ThrowException("No login to save an image to");
            }

            Tuple <string, string> ext = imageTypes.FirstOrDefault(x => string.Equals(x.Item1, file.ContentType, StringComparison.OrdinalIgnoreCase));

            if (ext == null)
            {
                es.ThrowInfoException("File to upload is not an image");
            }

            if (file.Length > options.Value.Images.InputSizeLimit * 1024 * 1024)
            {
                es.ThrowInfoException("File to upload exceeds a limit {0} Mb",
                                      options.Value.Images.InputSizeLimit.ToString());
            }

            string relativePath = Path.Combine(options.Value.AccountImagesPath, login, pageElement.ToString());
            string path         = Path.Combine(environment.WebRootPath, relativePath);

            Directory.CreateDirectory(path);

            List <string> files =
                Directory
                .GetFiles(path)
                .Where(x => Path.GetFileNameWithoutExtension(x) == file.FileName ||
                       Path.GetFileNameWithoutExtension(x) == file.FileName + "_")
                .ToList();

            bool add_ = files.Count > 0 && !files.Any(x => Path.GetFileNameWithoutExtension(x) == file.FileName + "_");

            files.ForEach(x => File.Delete(x));

            string fileName = file.FileName + (add_ ? "_" : "") + "." + ext.Item2;

            using (var fileStream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
            {
                if (file.Length > options.Value.Images.ConvertSizeLimit * 1024)
                {
                    reduceImageSize(file, fileStream);
                }
                else
                {
                    await file.CopyToAsync(fileStream);
                }
            }

            return(Path.Combine(relativePath, fileName).Replace('\\', '/'));
        }