Why with some browsers i get a wrong value for my Session object?
I am coding an asian language learning module for my mojoportal-based iphone-optimized website (work in progress, english resources are not fully translated: http://ilearn.dandandin.it/kanatrainer.aspx)
It's a simple "guess how to read this" game, with the right answer stored in a Session object.
I don't understand why, but, expecially using Safari, users will get someone else's Session value
This is an excerpt from the code (i removed some stuff, translated the variables)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
...
generateRandom();
}
}
protected void generateRandom()
{
int i, j = 0, livello = 5, chance = 0;
System.Random acaso = new Random();
...
while (j <= 0)
{
j = acaso.Next((Convert.ToInt32(TextBoxlunghezza.Text) + 1));
}
...
for (int k = 0; k < j; k++)
{
i = acaso.Next(livello);
Session["randomLetters"] += (globals.asianCharacters[i]);
...
}
...
}
protected void AnswerButton_Click(object sender, EventArgs e)
{
string compare = Server.HtmlEncode(InputTextBox.Text.ToLower());
if (compare == "")
{
Label1.Text = ("You did not write anything");
return;
}
if (Session["randomLetters"].ToString() != compare)
{
Label1.Text = ("Wrong!" + Session["randomLetters"]);
}
else
{
Label1.Text = ("Right!" + Session["randomLetters"]);
}
...
}
What happens in visual studio, with every browser:
randomLetters is "hello". User writes "hello" in the textbox, and "hello" is compared to "hello". Label says "Right! hello".
What happens in iis, only in webkit-based browsers:
randomLetters is "hello". User writes "hello" in the textbox, but "hello" is compared to "goodbye". Label says "Wrong! goodbye".
I don开发者_运维问答't understand how Session["randomLetters"] has changed
Public vs private code:
How are your storing the session state? Cookie? Database? so on... I have had many problems (usually with IE 8) with the way that the browser was caching the pages and the cookies. Usually, changing the respective setting in the browser fixed the problem. I don't know if that helps here. To make it more robust, I then have to find a way to notify the user when one of these settings is not right.
Using HiddenFields i "solved" the problem (but i don't like that way)
精彩评论