Generate x509 certificate in pem, cer and pfx and export public key

Generate x509 cerntifcate c:\Demo>openssl req -x509 -days 365 -newkey rsa:2048 -keyout my-key.pem -out -my-cert.pem Generating a RSA private key ………………………+++++ …………..+++++ writing new private key to ‘my-key.pem’ Enter PEM pass phrase: Verifying – Enter PEM pass phrase: —– You are about to be asked to enter information that will be incorporated into your certificate request. …

Send an image (stored as base64 string) inline in email

Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(“Base64 string”)); System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData); var imageToInline = new LinkedResource(streamBitmap, MediaTypeNames.Image.Jpeg); imageToInline.ContentId = “Pic1”; AlternateView avHtml = AlternateView.CreateAlternateViewFromString(mailMessage.Body, null, MediaTypeNames.Text.Html); avHtml.LinkedResources.Add(imageToInline); mailMessage.AlternateViews.Add(avHtml); public static string FixBase64ForImage(string Image) { System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image, Image.Length); sbText.Replace(“\r\n”, string.Empty); sbText.Replace(” “, string.Empty); return sbText.ToString(); }

UK Postcode Validation

Regular expression for JavaScript var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/; var result = regPostcode.test(SearchTerm); if (!result) { alert(“Please enter valid postcode”); return false; } Regular expression for C# string regPostcode = “([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}”; System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(regPostcode); if(!regx.IsMatch(address0.PostalCode)) { results.Add(new ValidationResult(String.Format(“Please enter valid postcode for {0}.”, addressRank))); } You can check the regular expression at http://www.regexplanet.com/ …

Remove illegal characters from file name or file path in c#

File cannot be saved if file name has illegal characters. So how to remove the illegal characters by a single line code as below. I use LINQ Querty for that. string fileName = “prakash C/O swami.xml”; fileName = System.IO.Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); Now the fileName is “rakash CO swami.xml”. The code removes slash …

Download / Upload Files on FTP Server

Require following ftp details where you want to upload the files: FTP URL: “ftp://ftp.yoursite.com/” FTP Username: username FTP Password: password FTP Port: 21 Upload File public string UploadFile(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = “”) { string PureFileName = new FileInfo(fileName).Name; String uploadUrl = String.Format(“{0}{1}/{2}”, FtpUrl, UploadDirectory, PureFileName); FtpWebRequest req = …

UnitOfWork Generic Repository

The repository and unit of work patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and can facilitate automated unit testing or test-driven development (TDD). UnitOfwork public class UnitOfWork …

Export DataTable to Excel/CSV C#

protected void btnExport_Click(object sender, EventArgs e) { DataTable dtTable = new DataTable(); DataRow dtRow; dtTable.Columns.Add(“SNo”, typeof(int)); dtTable.Columns.Add(“Address”, typeof(string)); for (int i = 0; i <= 9; i++) { dtRow = dtTable.NewRow(); dtRow[0] = i; dtRow[1] = "Address " + i.ToString(); dtTable.Rows.Add(dtRow); } Response.ContentType = "Application/x-msexcel"; Response.AddHeader("content-disposition", "attachment;filename=test.csv"); Response.Write(ExportToCSVFile(dtTable)); Response.End(); } private string ExportToCSVFile(DataTable dtTable) { …

Setup Project in Visual Studio

Create a new project Right click on Soultion Explorer of a Project. Select New Project. Select Project Type ‘Setup and Deployment’ under ‘Other Project Types’. Enter project name in text box in front of Name: Press OK How do I change the Product Name, Author, Manufaturer, etc? Select created a project from the Solution Explorer …