Articles

Affichage des articles du 2018

How to encrypt to a url safe string...

Hello,
Here's a helper that can be used to encrypt/decrypt ids that are url safe.
For example, if your user needs to download an invoice, it's clearly more secure to provide a url like 
http://yousite.com/download-invoice/TVZiSlU0a0VHWnlZR2NTOTRwYy9Sdz09
instead of:
http://yousite.com/download-invoice/13
As there can be a malicious user that will try to download invoices from others, or you don't want the user to know what the invoice id is.
Example, in your controller:


[ActionName("download-invoice")] publicFileResult downloadInvoice(string id)         { var decodeId =int.Parse(id.DecryptBase64()); var invoice = MainService.getInvoiceById(decodeId); if (invoice == null) thrownewHttpException(404, "Not Found"); return File(MainService.generateInvoicePDF(decodeId), "application/pdf");         }      

In your view:
<li>Invoice<atarget="_blank"href="/download-invoice/@Model.invoiceId.ToString().EncryptBase64()"><imgsrc=…

Html to Image...

Sometimes it's useful to generate an image from a url.


Here's how you can do it for free, without having to pay for expensive 3rd party components.


All you have to do is download wkhtmltoimage.exe from

https://wkhtmltopdf.org/downloads.html



       public static byte[] DownloadImage(string url)
        {
            return DownloadImage(url, 1024, 768);
        }

        public static byte[] DownloadImage(string url, int w, int h)
        {           
            var htmlToImageDir = Path.Combine("Your Path", "HtmlToImage");
            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                CreateNoWindow = false,
                FileName = Path.Combine(htmlToImageDir, "wkhtmltoimage.exe"),
                Arguments = s…