Showing posts with label http handler. Show all posts
Showing posts with label http handler. Show all posts

Tuesday, March 2, 2010

Redirect from Http to Https in ASP.NET

Hello frnds,
In my one project, there is a requirement for redirect some page from HTTP to HTTPS.
So find one great article that will help me. So here I post the link of that article and code of how its work.

http://www.xdevsoftware.com/blog/post/Redirect-from-Http-to-Https-in-ASPNET.aspx

Code:-
-------------------------------------------------------
Class File Name: SecurePages.cs
-------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;
#region "SecurePage Class"
public class SecurePage
{
string _path = "";
string _pathType = "";

public string Path
{
get { return this._path; }
set { this._path = value; }
}

public string PathType
{
get { return this._pathType; }
set { this._pathType = value; }
}
}
#region "SecurePath Class"
public class SecurePath
{

public static bool IsSecure(string path)
{
List lstPages = new List();

bool isSecure = false;

try
{
//Fill the list of pages defined in web.config
NameValueCollection sectionPages = (NameValueCollection)ConfigurationManager.GetSection("SecurePages");

foreach (string key in sectionPages)
{
if ((!string.IsNullOrEmpty(key)) && (!string.IsNullOrEmpty(sectionPages.Get(key))))
{
lstPages.Add(new SecurePage { PathType = sectionPages.Get(key), Path = key });
}
}

//loop through the list to match the path with the value in the list item
foreach (SecurePage page in lstPages)
{
switch (page.PathType.ToLower().Trim())
{
case "directory":
if (path.Contains(page.Path))
{
isSecure = true;
}
break;
case "page":
if (path.ToLower().Trim() == page.Path.ToLower().Trim())
{
isSecure = true;
}
break;
default:
isSecure = false;
break;
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

return isSecure;
}
}
#endregion

#endregion


/* Add New custom section in web.config */

<configuration>
<configSections>
<section name="SecurePages" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<SecurePages>
<add key="/admin" value="directory" />
<add key="/WebForm2.aspx" value="page" />
<add key="/WebForm1.aspx" value="page" />
<add key="/myDir/myDirSec" value="directory" />
</SecurePages>
</configuration>

Tuesday, July 7, 2009

HTTP Handlers for Image resize in ASP.NET

Hello frnds,
Here I put the code of image resize using http handler.

Page:PhotoResizeHandler.ashx

<%@ WebHandler Language="C#" Class="PhotoResizeHandler" %>

using System;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
public class PhotoResizeHandler : IHttpHandler
{
string _StrImage = string.Empty;
string _StrExt = string.Empty;
int _IntThumbWidth;
int _IntThumbHeight;

public void ProcessRequest(HttpContext context)
{
if (context.Request.Params["Height"] != null)
{
try
{
_IntThumbHeight = int.Parse(context.Request.Params["Height"]);
}
catch
{
_IntThumbHeight = 0;
}
}
if (context.Request.Params["Width"] != null)
{
try
{
_IntThumbWidth = int.Parse(context.Request.Params["Width"]);
}
catch
{
_IntThumbWidth = 0;
}
}
if (context.Request.Params["Image"] != null)
{
try
{
_StrImage = (context.Request.Params["Image"]).ToString();
}
catch
{
_StrImage = string.Empty;
}
}
if (context.Request.Params["Ext"] != null)
{
try
{
_StrExt = (context.Request.Params["Ext"]).ToString();
}
catch
{
_StrExt = string.Empty;
}
}
ResizeImage(_StrImage, _IntThumbWidth, _IntThumbHeight, _StrExt);

}

public bool IsReusable
{
get
{
return false;
}
}


private void ResizeImage(string MainImage, int thumbWidth, int thumbHeight, string ext)
{
try
{
System.Drawing.Image Image = System.Drawing.Image.FromFile(MainImage);
int srcWidth = Image.Width;
int srcHeight = Image.Height;

int w = thumbWidth, h = thumbHeight;
#region "Proposnal Ratio"
// proposnal resize of your image
if (srcHeight > thumbHeight)
{
w = srcWidth * thumbHeight / srcHeight;
}
else
h = srcHeight;

if (srcWidth > thumbWidth)
{
h = srcHeight * thumbWidth / srcWidth;
}
else
if (w > srcWidth)
w = srcWidth;
if (w > thumbWidth)
h = srcHeight * thumbWidth / srcWidth;
if (h > thumbHeight)
w = srcWidth * thumbHeight / srcHeight;

thumbWidth = (thumbWidth > w ? w : thumbWidth);
thumbHeight = (thumbHeight > h ? h : thumbHeight);
thumbWidth = (thumbWidth == 0 ? 1 : thumbWidth);
thumbHeight = (thumbHeight == 0 ? 1 : thumbHeight);

#endregion

Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(Image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

switch (ext.ToUpper())
{
case ".JPG":
case ".JPEG":
HttpContext.Current.Response.ContentType = "image/Jpeg";
bmp.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".GIF":
HttpContext.Current.Response.ContentType = "image/Gif";
bmp.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".PNG":
System.IO.MemoryStream MemStream = new System.IO.MemoryStream();
HttpContext.Current.Response.ContentType = "image/Png";
bmp.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);

MemStream.WriteTo(HttpContext.Current.Response.OutputStream);
//bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png );
break;
}
Image.Dispose();
}
catch (Exception Exc)
{
throw Exc;
}

}
}

Now you need to add one aspx page and create one asp:image controls there.

<asp:Image ID="img" runat="server" />

Then in cs file with on page load event, try to put this line
img.ImageUrl = "PhotoResizeHandler.ashx?Image=" + MainImage + "&Width=65&Height=65&Ext=" + ext;

Hopes this will help you.

Regards,
Kinjal Shah