captcha problem

Jan 3, 2011 at 5:38 PM

hi,

I have been tracking this project since long. It has been a very good learning experience. Today I was working on azure template and mvc web role and used your application (though the very old entity starter kit) and found an error in captcha.

It throws the error 'A generic error occurred in GDI+' at  image.Save((absolutDirectory + filename1), ImageFormat.Jpeg); in CaptchaImage method in userHelper.cs.

I checked the latest code and found userHelper.cs the same.

I searched for this error and got http://www.west-wind.com/Weblog/posts/8230.aspx . 

Thanks and best regards

Arnab C

 


Coordinator
Jan 3, 2011 at 8:47 PM
Edited Jan 3, 2011 at 8:48 PM
I did not experience this problem. but we will add a line of code in HtmlHelper CaptchaImage.
Thanks Arnab
******************************************************************************************************** 
public static HtmlString CaptchaImage(this HtmlHelper helper, int width, int height, string fontfamily)
        {
            CaptchaRepository captchaRepo = new CaptchaRepository();
            string solution = string.Empty;
            Bitmap _image = captchaRepo.mtGenerateImage(width, height, fontfamily, out solution);
            Bitmap image = new Bitmap(_image);

            HttpContext.Current.Session["antibotimage"] = solution;
            FileRepository file = new FileRepository();
            foreach (FileInfo x in file.GetAllFilesFromDirectory("()Content()captcha"))
            {
                if (x.CreationTime.Date < DateTime.Now.Date)
                    file.DeleteFile("()Content()captcha()" + x.Name);
            }
            string absolutDirectory = HttpContext.Current.Server.MapPath("/");
            string filename1 = "/Content/captcha/" + Guid.NewGuid() + ".jpeg";
            image.Save((absolutDirectory + filename1), ImageFormat.Jpeg);
            StringBuilder result = new StringBuilder();
            TagBuilder imgCaptcha = new TagBuilder("img");
            imgCaptcha.MergeAttribute("src", filename1);
            imgCaptcha.MergeAttribute("alt", "Captcha");
            imgCaptcha.AddCssClass("captcha");
            imgCaptcha.MergeAttribute("style", "width: " + width + "px; height: " + height + "px;");
            result.AppendLine(imgCaptcha.ToString());
            HtmlString htmlresult = new HtmlString(result.ToString());
            return htmlresult;
        }
Jan 9, 2011 at 2:36 PM

hi,

Its still not working on azure, it gives error on file.deleteFile (insufficient priviledges) as well as image.save (A generic error occurred in GDI+).

Please have a look, I'm also trying, If I find something, I will post here.

Regards

Arnab

Coordinator
Jan 10, 2011 at 6:20 AM

Yes, I faced the problem of lack of rights when creating a file into a Response.OutputStream

image.Save(Response.OutputStream, ImageFormat.Jpeg);

try

string absolutDirectory = HttpContext.Current.Server.MapPath("/");
string filename1 = "/Content/captcha/" + Guid.NewGuid() + ".jpeg";
image.Save((absolutDirectory + filename1), ImageFormat.Jpeg);

Regards

Oleg

 

Coordinator
Jan 11, 2011 at 4:16 AM

Code for mytripmvc:

MytripMvcController.cs add method:

 public void Captcha(int id, int id2, string id3)
        {
            CaptchaRepository captchaRepo = new CaptchaRepository();
            string solution = string.Empty;
            Bitmap _image = captchaRepo.mtGenerateImage(id, id2, id3, out solution);                        
            Session["antibotimage"] = solution;
            Bitmap image = new Bitmap(_image);
            Response.ContentType = "image/jpeg";
            image.Save(Response.OutputStream, ImageFormat.Jpeg);
            _image.Dispose();
            image.Dispose();
            Response.End();
        }

UserHelper.cs modifed method:

public static HtmlString CaptchaImage(this HtmlHelper helper, int width, int height, string fontfamily)
        {
            //CaptchaRepository captchaRepo = new CaptchaRepository();
            //string solution = string.Empty;
            //Bitmap _image = captchaRepo.mtGenerateImage(width, height, fontfamily, out solution);
            //Bitmap image = new Bitmap(_image);
            //HttpContext.Current.Session["antibotimage"] = solution;
            //FileRepository file = new FileRepository();
            //foreach (FileInfo x in file.GetAllFilesFromDirectory("()Content()captcha"))
            //{
            //    if (x.CreationTime.Date < DateTime.Now.Date)
            //        file.DeleteFile("()Content()captcha()" + x.Name);
            //}
            //string absolutDirectory = HttpContext.Current.Server.MapPath("/");
            //string filename1 = "/Content/captcha/" + Guid.NewGuid() + ".jpeg";
            //image.Save((absolutDirectory + filename1), ImageFormat.Jpeg);
            //StringBuilder result = new StringBuilder();
            //TagBuilder imgCaptcha = new TagBuilder("img");
            //imgCaptcha.MergeAttribute("src", filename1);
            //imgCaptcha.MergeAttribute("alt", "Captcha");
            //imgCaptcha.AddCssClass("captcha");
            //imgCaptcha.MergeAttribute("style", "width: " + width + "px; height: " + height + "px;");
            //result.AppendLine(imgCaptcha.ToString());
            StringBuilder result = new StringBuilder();
            TagBuilder imgCaptcha = new TagBuilder("img");
            imgCaptcha.MergeAttribute("src", "/Theme/"+ThemeSetting.theme()+"/images/empty.gif");
            imgCaptcha.MergeAttribute("alt", "Captcha");
            imgCaptcha.AddCssClass("captcha");
            imgCaptcha.MergeAttribute("style", "width: " + width + "px; height: " + height + "px; background-image:url('/MytripMvc/Captcha/" + width + "/" + height + "/" + fontfamily + "');");
            result.AppendLine(imgCaptcha.ToString());
            HtmlString htmlresult = new HtmlString(result.ToString());
            return htmlresult;
        }
Working on hosting http://mytripmvc.net/Account/Register

 

Coordinator
Jan 11, 2011 at 10:39 PM

super solution :)   convert bitmap to byte[]

MytripMvcController.cs add/modifed method:

 public FileContentResult Captcha(int id, int id2, string id3)
        {
            CaptchaRepository captchaRepo = new CaptchaRepository();
            string solution = string.Empty;
            Bitmap _image = captchaRepo.mtGenerateImage(id, id2, id3, out solution);
            byte[] bytes = (byte[])TypeDescriptor.GetConverter(_image).ConvertTo(_image, typeof(byte[]));
            Session["antibotimage"] = solution;
            return File(bytes, "image/jpeg");
        }
Working on hosting http://mytripmvc.net/Account/Register
Jan 12, 2011 at 5:25 AM

Thanks for the reply.

just one thing : where is the captcha method being called from


Regards
Arnab
           
Coordinator
Jan 12, 2011 at 11:06 AM

Core/Models/ValidationAttribute.cs

public class CaptchaErrorAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var captcha = (String)value;
            bool result = true;
            if (!CoreSetting.Development() && UsersSetting.unlockCaptcha() && MytripUser.HashCaptcha(captcha) != HttpContext.Current.Session["antibotimage"].ToString())
            {
                result = false;
            }

            return result;
        }

    }
You ask me about it?

Regards

Oleg

Jan 13, 2011 at 2:33 AM

public
FileContentResult Captcha(int id, int id2, string id3) { CaptchaRepository captchaRepo = new CaptchaRepository(); string solution = string.Empty; Bitmap _image = captchaRepo.mtGenerateImage(id, id2, id3, out solution); byte[] bytes = (byte[])TypeDescriptor.GetConverter(_image).ConvertTo(_image, typeof(byte[])); Session["antibotimage"] = solution; return File(bytes, "image/jpeg"); }

From where is this method "Captcha" being called? 
Thanks and regards
Arnab
Coordinator
Jan 13, 2011 at 3:57 AM
public static HtmlString CaptchaImage(this HtmlHelper helper, int width, int height, string fontfamily)
        {
            StringBuilder result = new StringBuilder();
            TagBuilder imgCaptcha = new TagBuilder("img");
            imgCaptcha.MergeAttribute("src", "/Theme/"+ThemeSetting.theme()+"/images/empty.gif");
            imgCaptcha.MergeAttribute("alt", "Captcha");
            imgCaptcha.AddCssClass("captcha");
            imgCaptcha.MergeAttribute("style", "width: " + width + "px; height: " + height + 
"px; background-image:url('/MytripMvc/Captcha/" + width + "/" + height + "/" + fontfamily + "');");
            result.AppendLine(imgCaptcha.ToString());
            HtmlString htmlresult = new HtmlString(result.ToString());
            return htmlresult;
        }
Background:
/MytripMvc/Captcha/" + width + "/" + height + "/" + fontfamily 
Jan 13, 2011 at 12:11 PM

Thanks, 

But its not working, its not even going to the Captcha method.

When I look at page source I find <img alt="Captcha" class="captcha" src="/Content/images/empty.gif" style="width: 202px; height: 60px; background-image:url(&#39;/Account/Account/Captcha/202/60/Times New Roman&#39;);"></img>

Regards

Arnab

Coordinator
Jan 13, 2011 at 4:36 PM

(&#39;/Account/Account/Captcha/202/60/Times New Roman&#39;

Controller Account?

method Account(captcha,202,60,"times new roman")?

You replace?

Oleg

Jan 14, 2011 at 7:35 AM
Edited Jan 14, 2011 at 8:18 AM

Area =Account

Controller=Account

Method= Captcha (Same that you gave)

I shifted the method to other controllers as well but that has not helped.

Regards

Arnab

Coordinator
Jan 14, 2011 at 11:38 AM

I do not use "Area"

this should not prevent ... can send code to  mytripmvc@gmail.ru?

 

Jan 15, 2011 at 7:59 AM

I created a very simple implementation, even that gave the same problem.

sent the code : 

What I have done:
Empty mvc project created.
 Added userhelper and captcharepository classes
Created two controllers home and captcha
added captcha method to captcha controller
Created a view index from index method in homecontroller.
added the namespace of htmlhelper in web.config
added html.captcha code in index.aspx view 

Regards

Arnab

 

Jan 15, 2011 at 8:36 AM
Edited Jan 15, 2011 at 8:37 AM

The email bounced

error: Recipient address rejected: User unknown in virtual mailbox table

Coordinator
Jan 15, 2011 at 9:25 AM

sorry mytripmvc@gmail.com

regards

Oleg

Coordinator
Jan 17, 2011 at 6:43 AM

Global.asax.cs

Add MapRoute

routes.MapRoute(

                "Default2", // Route name

                "{controller}/{action}/{id}/{id2}/{id3}", // URL with parameters

                new { controller = "Home", action = "Index", id = UrlParameter.Optional,

                      id2 = UrlParameter.Optional,

                      id3 = UrlParameter.Optional

                } // Parameter defaults

            );

Or

/captcha/Captcha/" + width + "?id2=" + height + "&id3=" + fontfamily + "

 

Oleg