How to log POSTed forms submissions? - asp.net

Back in the ASP classic days when i needed to write out the name/value pairs of forms submitted by POST i thru this loop into the page:
on error resume next
for each x in Request.Form
Response.AppendToLog x & "=" & Request(x)
next
It threw all the form fields and values into the log just as GETs are. Does IIS7 .net give me any better method? (this is for the dev/testing portion of the project i don't have any concern about the space or cycles used to accomplish this).
thx

You can create an http module to log all posts. It allows you to log outside of the pages, a single point of logging instead of having to add the logic to all pages where you want to log activity.
Here you have some of the code. You would have to avoid logging viewstate since is tons of useless information. So you have to add some logic to achieve this.
public class ActivityLogModule: IHttpModule
{
public void Init(HttpApplication application)
{
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (RecordActivity(context))
{
ActivityLogger.Instance.Log(application.Context.User.Identity.Name,
application.Context.Request.Url.AbsoluteUri,
application.Context.Request.Form.ToString());
}
}
public void Dispose(){}
protected bool RecordActivity(HttpContext context)
{
if (!context.Request.RequestType.Equals("POST"))
{
return false;
}
return true;
}
}

You could have something like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
LogPostValues();
}
private void LogPostValues()
{
string logPath = #"C:\PostedValuesLog.txt";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Logging: {0}", Request.Path);
sb.Append("Form Values");
foreach (string key in Request.Form)
{
string val = Request.Form[key];
sb.AppendFormat("{0} = {1}<br/>", key, val);
}
sb.Append(Environment.NewLine);
sb.Append("QueryString Values");
foreach (string key in Request.QueryString)
{
string val = Request.QueryString[key];
sb.AppendFormat("{0} = {1}<br/>", key, val);
}
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
File.AppendAllText(logPath, sb.ToString());
}
This is a crude method though and shouldn't really be used in production code. However, as this is just for development & testing, it should suffice to track what data is being posted to your page via the querystring and form.

Related

Wierd behavior while many clients generate cache

I show you my problem by simple code snippet.
This is popular scenario. Users load our page when there is no cache so we generate one. In my code example this take 120 seconds to save cache and before this i inrement static variable.
My qustion is why static variable "i" doesn't increment when i open this page many times in the same moment and cache is null.
public partial class _Default : Page
{
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
int i;
var cache = Cache.Get("cache") as string;
if (string.IsNullOrEmpty(cache))
{
i = GenerateCache();
}
else
{
i = Convert.ToInt32(cache);
}
Response.Write(i.ToString());
}
public int GenerateCache()
{
var sw = new Stopwatch();
sw.Start();
++i;
Response.Write(i+"<br>");
while (sw.ElapsedMilliseconds < 1000 * 120) { }
Cache.Insert("cache", i.ToString());
return i;
}
}
Because you have a bug by declaring again the i on the PageLoad
protected void Page_Load(object sender, EventArgs e)
{
int i; // <----- here, this is probably bug and you must remove this line
also you need some kind of locking to avoid multiple calls at the same moment, even tho you saved by the lock of the page session for the moment.

How can I use my own random captcha in ASP.NET?

For hands on experience, I am generating random numbers and compare their sums on submission, but every time, I press button, random numbers are changing, How can I stop happening this? I considered different experiments, but not able to do that.
Here is my class code for random numbers:
public class Captcha
{
int num1, num2, total;
public int N1
{
get
{
return num1;
}
}
public int N2
{
get
{
return num2;
}
}
public int Total
{
get
{
return total;
}
}
Random r = new Random();
public Captcha()
{
Random r = new Random();
num1 = r.Next(1, 11) * 10;
num2 = r.Next(1, 11);
total = num1 + num2;
}
}
Now, I am using this one on Page_Load event like this :
Captcha c = new Captcha();
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = c.N1.ToString();
Label3.Text = c.N2.ToString();
}
How can I accomplished this one?
Thanks.
Since you are creating object of Captcha outside the Page Load event, it will get re-created whenever a call to the page happens.
You might be aware that internet is state-less so whenever there is a call to the Page, a new page gets generated, and hence new Captcha object. So you need to maintain the Captcha object in some state management like view state.
Your code should be something like this.
Captcha c;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
c = new Captcha();
Label2.Text = c.N1.ToString();
Label3.Text = c.N2.ToString();
ViewState["c"] = c;
}
}
Then on button click you need to type cast the viewstate back to "c". Something like this.
//button click
c = (Captcha)ViewState["c"];
if(TextBox1.Text.Equals......)
Maybe you need to mark your Captcha class as serializable as it might throw error.
I hope this will help.
Regards,
Samar

Posting data between Asp.Net Web Pages

I have a list of strings, which are generated on a imagebutton_click method. I want to be able to use this list in another webpage.
How ever im not quite sure how to go about posting it between the two pages.
I have the following code below:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
RadGrid rg = RadGrid1;
//Get selected rows
GridItemCollection gdc = (GridItemCollection)rg.SelectedItems;
foreach (GridItem gi in gdc)
{
if (gi is GridDataItem)
{
GridDataItem gdi = (GridDataItem)gi;
if (!string.IsNullOrEmpty(gdi["Email"].Text))
{
string client = gdi["Email"].Text;
//Creating a List of Clients to be Emailed
emailList.Add(email);
}
}
//Enable the Prepare Email Page
PageView2.Selected = true;
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
if (emailList.Count != 0)
{
for (int i = 0; i < emailList.Count; i++)
{
_to = emailList[i].ToString() + ";";
}
}
else
{
_to = emailList[1].ToString();
}
//Processing Client Email
string _from = sec.GetCurrentUserEmail("test");
string _cc = "";
string _subject = SubjectTB.Text;
string _body = EmailEditor.Content;
string _tempTo = sec.GetCurrentUserEmail("temp");
string _msg = sec.SendMail(_tempTo, _cc, _from, _subject, _body, "");
if (_msg == "success")
{
//Thank the user and record mail was delivered sucessfully
TestPanel.Visible = true;
}
}
How do I get the values of emailList to be passed through to ImageButton2_click(object sender, ImageClickEventArgs e). Currently it just passes through a null value. I gather I need to use HTML forms to do the request. Thanks.
I'm guessing that emailList is a private variable? Wouldn't you be able to add that to the LoadControlState and SaveControlState so that it'll be available for ImageButton2_Click later?
Here is an example: http://msdn.microsoft.com/en-us/library/system.web.ui.control.loadcontrolstate%28v=vs.80%29.aspx
Another possibility is hidden field, that might be the simplist way, but not as secure.
You can get a good idea about state management in ASP.Net here. For your case if button1 and button2 are in the same aspx page, Viewstate would be a good idea. If they are in diferent pages then use Session state management.

load different css for site localization

I need to load different css file depending on the language that the user selects. I need to do this only in my master page.
If you are using the built-in themes and globalization support you could use a httpModule: (untested)
public class PageModule : IHttpModule
{
public void Dispose()
{
}
public void Init(System.Web.HttpApplication context)
{
context.PreRequestHandlerExecute += Application_PreRequestHandlerExecute;
}
public void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
//Adds a handler that executes on every page request
HttpApplication application = default(HttpApplication);
application = (HttpApplication)sender;
Page page = application.Context.CurrentHandler as Page;
if ((page != null))
page.PreInit += Page_PreInit;
}
public void Page_PreInit(object sender, EventArgs e)
{
//If current context has no session then abort
if (HttpContext.Current.Session == null)
return;
//Get current page context
Page page = (Page)sender;
switch (page.Culture) {
case "en-US":
page.Theme = "en-USTheme";
break;
case "fr-FR":
page.Theme = "fr-FRTheme";
break;
default:
page.Theme = "DefaultTheme";
break;
}
}
}
you could write the selected language in a cookie. Then in you master page inspect the value saved in the cookie and assign the correct stylesheet.

SmtpClient.SendAsync bug in ASP.NET 2.0

I may be wrong, but if you are working with SmtpClient.SendAsync in ASP.NET
2.0 and it throws an exception, the thread processing the request waits
indefinitely for the operation to complete.
To reproduce this problem, simply use an invalid SMTP address for the host
that could not be resolved when sending an email.
Note that you should set Page.Async = true to use SendAsync.
If Page.Async is set to false and Send throws an exception the thread
does not block, and the page is processed correctly.
TIA.
Note that you should set Page.Async = true to use SendAsync.
Please explain the rationale behind this. Misunderstanding what Page.Async does may be the cause of your problems.
Sorry, I was unable to get an example working that reproduced the problem.
See http://msdn.microsoft.com/en-us/magazine/cc163725.aspx (WICKED CODE: Asynchronous Pages in ASP.NET 2.0)
EDIT: Looking at your code example, I can see that you're not using RegisterAsyncTask() and the PageAsyncTask class. I think you must do this when executing asynchronous tasks on a Page where #Async is set to true. The example from MSDN Magazine looks like this:
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
RegisterAsyncTask(task);
}
Inside BeginAsyncOperation, then, should you send a mail asynchronously.
RegisterAsyncTask could not be used.
Look at the BeginEventHandler delegate:
public delegate IAsyncResult BeginEventHandler(
Object sender,
EventArgs e,
AsyncCallback cb,
Object extraData
)
It should return an IAsyncResult.
Now look at the SmtpClient.SendAsync function :
public void SendAsync(
MailMessage message,
Object userToken
)
There is no return value.
Anyway this is working fine, as long as SmtpClient.SendAsync does not throw an exception.
Here is mine. Give it a try.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Using an incorrect SMTP server
SmtpClient client = new SmtpClient(#"smtp.nowhere.private");
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("someone#somewhere.com",
"SOMEONE" + (char)0xD8 + " SOMEWHERE",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("someone#somewhere.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
try
{
client.SendAsync(message, userState);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(string.Format("Send Error [{0}].", ex.InnerException.Message));
}
finally
{
}
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;
if (e.Cancelled)
{
Response.Write(string.Format("[{0}] Send canceled.", token));
}
if (e.Error != null)
{
Response.Write(string.Format("[{0}] {1}", token, e.Error.ToString()));
}
else
{
Response.Write("Message sent.");
}
}
}

Resources