API Questions on Kentico API.
Version 6.x > API > Httphandler for unauthenticated users View modes: 
User avatar
Member
Member
guillaume.marquilly-tv5monde - 4/29/2012 10:45:27 PM
   
Httphandler for unauthenticated users

Hello,

I am currently developping a very large webpart and one of its feature is to display some pictures from an external database, using HttpHandlers.

When the visitor is authenticated, i do not have any issues, but when the visitor is anonymous, the codebehind of my HttpHandler can not be reached.


Please have a look to the code :

BDDImageProgram.ashx (Location: My webpart folder)

<%@ WebHandler Language="C#" CodeBehind="BDDImageProgram.ashx.cs" Class="CMS.CustomHandlers.BDDImageProgram" %>

BDDImageProgram.ashx.cs (Location : App_Code)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using TVProgram.BAL;

namespace CMS.CustomHandlers
{
/// <summary>
/// Summary description for ImageAsync
/// </summary>
public class BDDImageProgram : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
Guid idProgram = Guid.Parse(context.Request.QueryString["idProgram"].ToString());

Program program = BDD.getProgram(idProgram);

if (program.Picture != null)
{
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(program.Picture.Data.ToArray());
context.Response.Flush();
}
}
catch (Exception ex)
{
renderError(context, ex.Message);
}
}
public bool IsReusable
{
get
{
return true;
}
}

private void renderError(HttpContext context, string msg)
{
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
// calculate the image width by message length
Bitmap bitmap = new Bitmap(7 * msg.Length, 30);
Graphics g = Graphics.FromImage(bitmap);
// create a background filler
g.FillRectangle(new SolidBrush(Color.DarkRed), 0, 0, bitmap.Width, bitmap.Height);
// draw our message
g.DrawString(msg, new Font("Tahoma", 10, FontStyle.Bold), new SolidBrush(Color.White), new PointF(5, 5));
// stream it to the output
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}



User avatar
Member
Member
kentico_michal - 5/1/2012 3:35:39 AM
   
RE:Httphandler for unauthenticated users
Hi,

Do you receive any error message?

What happens if you create some test page in the same folder (My webpart folder) as the handler and try to access it as anonymous user?

Can you see the page?

If not, could you please allow anonymous users to see those files as described in this article: ASP.NET Authorization

Best regards,
Michal Legen