PDA

View Full Version : حرفه ای: مشکل در شناسایی session در ارتباط سه گانه صفحه به وب یوزر کنترل و یک هندلر جهت ایجاد تصویر امنیتی



aynehband
یک شنبه 18 دی 1390, 18:34 عصر
سلام
من یک captcha نوشتم ، که یک usercontrol یک هندلر را فراخوانی کنه که تصویر را تولید کنه، با querystring کار میکنه، با session نه.

این کد با session گیر میده. با httpcontext.current.user.session هم گیر میده.
ضمنا :D اون پروپرتی (_SecCode) را هم نمیشه بخونم. مقدارش همیشه خالیه. مجبورم با session چک کنم.



کد web user control


private string _SecCode ;
public string SecCode
{
get { return _SecCode; }
set { _SecCode = value; }
}

protected void Page_Load(object sender, EventArgs e)
{

this.PreRender+=new EventHandler(Controls_site_secimage_PreRender);

}

protected void Controls_site_secimage_PreRender(object sender, EventArgs e)
{
Button1_Click(null, null);


}



protected void Button1_Click(object sender, EventArgs e)
{
String ABC = "QWERTYUI89PZSXDCFGBJHN15687420";
string last_text="";
Random rand = new Random();
int i;
for (i = 0; i <= 3; i++)
{
last_text +=ABC.Substring( Convert.ToInt32(rand.Next(1,30)),1);
}
Session["seccode"] = last_text;
_SecCode = last_text;
Image1.ImageUrl = "ShowSecImage.ashx" ;


}



کد فایل ShowSecImage.ashx


public void ProcessRequest(HttpContext context)
{
string file="";
HttpResponse r = context.Response;
r.ContentType = "image/png";

file =context.Session["seccode"].ToString();
string filename = "back_Sec.png";




int height = 100;// Convert.ToInt32(context.Request["height"]);
int width = 100; //Convert.ToInt32(context.Request["width"]);
try
{
if ((context.Request["height"] != null) )
{
if( (Convert.ToInt32(context.Request["height"]) > 20) && (Convert.ToInt32(context.Request["height"])< 600))
height = Convert.ToInt32(context.Request["height"]);
else height = 50;


}
else{height = 50;}
if ((context.Request["width"] != null))
{if( (Convert.ToInt32(context.Request["width"]) > 20) && (Convert.ToInt32(context.Request["width"])< 800))
width = Convert.ToInt32(context.Request["width"]);
else width = 150;

}
else { width = 150; }


}
catch {


}

using (Image original = Image.FromFile(HttpContext.Current.Server.
MapPath( filename)))
{
using (Bitmap resized = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
using (Graphics g = Graphics.FromImage(resized))
{
Image logg=Image.FromFile( HttpContext.Current.Server.MapPath(System.Configur ation.ConfigurationManager.AppSettings["Paths_doc_image"].ToString() + "logo.png"));
Image FrameImage = Image.FromFile(HttpContext.Current.Server.MapPath( System.Configuration.ConfigurationManager.AppSetti ngs["Paths_doc_image"].ToString() + "frame.png"));
int width_border, heightborder;
width_border = Convert.ToInt32(width/14);
heightborder = Convert.ToInt32(height / 14);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

g.DrawImage(original, new Rectangle(new Point(0, 0), new Size(width, height)));

g.DrawString(file, new Font("tahoma", 25, FontStyle.Regular), Brushes.Black, new Point(0 , 0));


var resizedStream = new MemoryStream();
resized.Save(resizedStream, ImageFormat.Jpeg);

context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(resizedStream.GetBuff er());
context.Response.End();
}
}
}
}

public bool IsReusable {
get {
return false;
}
}

مهدی کرامتی
سه شنبه 20 دی 1390, 02:46 صبح
چه گیری به Session میده؟

aynehband
سه شنبه 20 دی 1390, 06:49 صبح
کلا میگه وجود نداره. مقدارش null می باشد. البته توی کد هنگام pre render وب یوزر کنترل نشست مقدار دهی میشه، اما صفحه هندلر نمی تونه session را پیدا کنه. اگر با query string باشه کاملا صحیح کار میکنه، اما به دلیل اینکه به عنوان یک پارامتر ارسال میشه خود کد امنیتی در صفحه وجود داره و قابل واکشی است، مگر اینکه رمزش کنم. اما به هر حال می خواستم ببینم مشکل session چیست؟
و مشکل بعدی خالی بودن مقدار پارامتر در هنگام چک کردن است.
خواندن با نشست
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(Session["seccode"].ToString() + "<br/>");
if (TextBox3.Text.Trim() == Session["seccode"].ToString())
Response.Write("OK");
else
Response.Write("Error");
}

خواندن با پروپرتی secimage1.SecCode که همیشه مقدارش غلطه زیرا خالیه
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(secimage1.SecCode+ "<br/>");
if (TextBox3.Text.Trim() == secimage1.SecCode)
Response.Write("OK");
else
Response.Write("Error");
}
اگر در مورد دوم هم نظری دارند دوستان بفرمایند.

مهدی کرامتی
سه شنبه 20 دی 1390, 13:26 عصر
کلاسی Handler شما برای این که به Session دسترسی داشته باشد میبایست علاوه بر IHttpHandler از اینترفیس IRequiresSessionState نیز به ارث ببرد. مثال:
public class CodeHandler : IHttpHandler, IRequiresSessionState
کد کامل:
public class CodeHandler : IHttpHandler, IRequiresSessionState
{

public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
if (context.Session["Code"] != null)
{
context.Response.Write("The Code Is:" + context.Session["Code"].ToString());
}
else
{
context.Response.Write("Session Code is not set");
}

}

public bool IsReusable
{
get
{
return false;
}
}
}

aynehband
چهارشنبه 21 دی 1390, 17:04 عصر
با تشکر از راهنمایی شما،مشکل اول مرتفع شد اما دومی هنوز خیر، در مورد دوم هم راهنمایی بفرمایید ممنون میشوم.

aynehband
جمعه 12 خرداد 1391, 11:36 صبح
در مورد حل خواندن یک مقدار از یوزر کنترل این روش مناسبی است.http://www.dotnetfunda.com/articles/article97.aspx