奇怪的问题,在Safari和Asp.net饼干 [英] Strange problem with cookies in Safari and Asp.net
问题描述
我有我的登录页在Asp.net才会出现这种问题的Safari。在一个奇怪的问题。
当用户通过验证我从数据库中提取用户的名称(在数据库中的字段是UTF8),并将其保存在cookie中。问题是,当用户有特殊字符的名字我重定向到哪里,我从没有被记录在附带的页面。例如,穆勒工作正常,并且用户已经登录,但不是默勒。
此外,这仅与野生动物园发生,当我有在名称中的特殊字符。不工作的行是:Response.Cookies [用户信息] [名称] = getNameFromUserid(用户ID);
这是我的code:
字符串userid = validUserWithEmail(TextBoxEmail.Text,TextBoxPassword.Text);
如果(用户ID!= NULL){
//有效的用户
。Response.Cookies [用户信息] =到期DateTime.Now.AddDays(30);
Response.Cookies [用户信息] [名称] = getNameFromUserid(用户ID); FormsAuthentication.RedirectFromLoginPage(用户ID,CheckBoxPersistCookie.Checked);
}
其他
{
//不是有效的用户显示信息为用户或某事
}
Safari浏览器将不设置与非ASCII字符cookies在他们的价值和其他浏览器可以在它们的显示非ASCII字符未predictable。作为分号也没有在cookie值允许任何浏览器我会建议使用UrlEn code / UrlDe code。
如果你只是写cookie并没有在现场读取控制/显示值放在URLDe code你也可以做这样的事情:
ckCookie.Value =(Server.HtmlEn code(strSpecialCharacters))更换。(;,);
这将确保满弦在饼干和Safari,Chrome浏览器,Firefox和IE的设置仍将识别HTML codeS即使没有;和阅读时不需要解码。
有关的cookie规格较长的回答,请参阅:<一href=\"http://stackoverflow.com/questions/1969232/allowed-characters-in-cookies/1969339#1969339\">Allowed在cookie中的字符
I have a strange problem on my login page in Asp.net this problem only happens with Safari.
When the user is validated I fetch the name of the user from the database (the field in the database is UTF8) and save it in a cookie. The problem is that when the user has a name with special characters I get redirected to the page where I came from without being logged in. For example "Moller" works fine and the user is logged in but not "Møller".
Again this is only happening with Safari and when I have special characters in the name. The row that isn't working is: Response.Cookies["userInfo"]["name"] = getNameFromUserid(userid);
This is my code:
string userid = validUserWithEmail(TextBoxEmail.Text, TextBoxPassword.Text);
if (userid != null) {
//VALID USER
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["userInfo"]["name"] = getNameFromUserid(userid);
FormsAuthentication.RedirectFromLoginPage(userid, CheckBoxPersistCookie.Checked);
}
else
{
//NOT A VALID USER SHOW A MESSAGE FOR THE USER OR SOMETHING
}
Safari will not set cookies with non-ASCII characters in their value and other browsers can be unpredictable in how they display non-ASCII characters. As semi-colon is also not allowed in cookie values for any browser I would recommend using UrlEncode/UrlDecode.
If you are just writing the cookie and do not have control over the site reading/displaying the value to put in the URLDecode you can also do something like this:
ckCookie.Value = (Server.HtmlEncode( strSpecialCharacters )).Replace(";","");
This will ensure the full string is set in the cookie and Safari, Chrome, Firefox and IE will still recognize the html codes even without the ; and does not require decoding when read.
For a longer answer on cookie specs see: Allowed characters in cookies
这篇关于奇怪的问题,在Safari和Asp.net饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!