How To Redirect HTTP to HTTPS using C#

HttpToHTTPs

Hello Readers,

While developing web application many of us must have observed that after deploying to web server, web page url starts with http but not the secured one https.

So if we want to redirect url from HTTP to HTTPS there is a very simple solution in C#.

Just copy and paste the below code in your “Global.asax.cs” file and you are all done.

Continue reading

Download a PDF file stored in a network location

In View page (.cshtml) Create a Link using anchor tag and define href field as:

href = ‘@Url.Action(“Download”, “YourControllerName”,new{filePath = “”})’

public FileResult Download(string filePath)
{
MemoryStream ms = new MemoryStream();

byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
string fileNAme = Path.GetFileName(filePath);

string tmpPath = Path.GetTempPath();
tmpPath = tmpPath + fileNAme;

PdfReader pdfReader = new PdfReader(fileBytes);

PdfReader.unethicalreading = true;
FileStream sourceFileStream = new FileStream(tmpPath, FileMode.Create, FileAccess.Write);
PdfStamper pdfStream = new PdfStamper(pdfReader, sourceFileStream);
string waterMark= "Confidential"
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(page);
PdfContentByte pdfData = pdfStream.GetUnderContent(page);
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.6F;
pdfData.SetGState(graphicsState);
pdfData.SetColorFill(BaseColor.GRAY);
pdfData.BeginText();
pdfData.ShowTextAligned(Element.ALIGN_CENTER, waterMark, pageRectangle.Width / 2, pageRectangle.Height / 2, 45);
pdfData.EndText();
}
pdfStream.FormFlattening = true;
pdfStream.Close();
pdfStream.Dispose();
sourceFileStream.Close();
sourceFileStream.Dispose();

byte[] byteArray = System.IO.File.ReadAllBytes(tmpPath);
System.IO.File.Delete(tmpPath);
ms.Write(byteArray, 0, byteArray.Length);
ms.Position = 0;

return new FileStreamResult(ms, "application/pdf");
}

To use above code you need iTextSharp dll. you can install it using Nuget as: Install-Package iTextSharp

References :
<ul>
	<li><a https://developers.itextpdf.com/itextsharp-net</a></li>
</ul>

How to convert local path to UNC (Universal) File Path in C#


In your controller initialize class obj:

LocalPathToUNCPathConversion obj = new LocalPathToUNCPathConversion();
string localPath = "filename"; //give locally mapped network drive file path
string UNCFilePath = obj.GetUniversalName(localPath);

write logic in a separate class file named as LocalPathToUNCPathConversion.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace YourProjectName
{
    public class LocalPathToUNCPathConversion
    {
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.U4)]
static extern int WNetGetUniversalName(
string lpLocalPath,
[MarshalAs(UnmanagedType.U4)] int dwInfoLevel,
IntPtr lpBuffer,
[MarshalAs(UnmanagedType.U4)] ref int lpBufferSize);

const int UNIVERSAL_NAME_INFO_LEVEL = 0x00000001;
const int REMOTE_NAME_INFO_LEVEL = 0x00000002;

const int ERROR_MORE_DATA = 234;
const int NOERROR = 0;

public string GetUniversalName(string localPath)
{
// The return value.
string retVal = null;

// The pointer in memory to the structure.
IntPtr buffer = IntPtr.Zero;

// Wrap in a try/catch block for cleanup.
try
{
// First, call WNetGetUniversalName to get the size.
int size = 0;

// Make the call.
// Pass IntPtr.Size because the API doesn't like null, even though
// size is zero. We know that IntPtr.Size will be
// aligned correctly.
int apiRetVal = WNetGetUniversalName(localPath, UNIVERSAL_NAME_INFO_LEVEL, (IntPtr)IntPtr.Size, ref size);

// If the return value is not ERROR_MORE_DATA, then
// raise an exception.
if (apiRetVal != ERROR_MORE_DATA)
// Throw an exception.
throw new Win32Exception(apiRetVal);

// Allocate the memory.
buffer = Marshal.AllocCoTaskMem(size);

// Now make the call.
apiRetVal = WNetGetUniversalName(localPath, UNIVERSAL_NAME_INFO_LEVEL, buffer, ref size);

// If it didn't succeed, then throw.
if (apiRetVal != NOERROR)
// Throw an exception.
throw new Win32Exception(apiRetVal);

// Now get the string. It's all in the same buffer, but
// the pointer is first, so offset the pointer by IntPtr.Size
// and pass to PtrToStringAnsi.
retVal = Marshal.PtrToStringAuto(new IntPtr(buffer.ToInt64() + IntPtr.Size), size);
retVal = retVal.Substring(0, retVal.IndexOf('\0'));
}
finally
{
// Release the buffer.
Marshal.FreeCoTaskMem(buffer);
}

// First, allocate the memory for the structure.

// That's all folks.
return retVal;
}
}
}
 

Convert local path to UNC (Universal) File Path using Java Script ActiveXObject