C# Cookie 不保存其信息
Scenerio - 我正在编写一个网络应用程序,它根据您使用的登录框(数据库1和数据库2)将您连接到相应的数据库。我已将 web.config 设置为保存两个连接字符串。在每个框的 On_Authenticate 方法中,我检查以对用户进行身份验证,然后将基于登录框的字符串发送到名为“Authenticate user”的类中的公共变量。此公共变量检查 cookie 并从变量中获取连接字符串名称,如果 cookie 为空,则设置一个新的 cookie,使所有现有 cookie 过期并设置 cookie 值。
现在,一旦完成,每次访问数据库(使用 LINQ2SQL)时,我都会创建一个数据上下文并发送 AuthenticatedUser.ConnectionString。这将检查 cookie 并发送获取连接字符串以用于提取数据。这里是 AuthenticatedUser 类...
public class AuthenticatedUser : System.Web.UI.Page
{
public static string ConnectionString
{
get
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
return GetConnectionStringFromName(myCookie);
}
set
{
if (HttpContext.Current.Request.Cookies["connectionString"] != null)
{
ExpireCookies(HttpContext.Current);
}
var allCookies = HttpContext.Current.Request.Cookies.AllKeys;
HttpCookie cookie = new HttpCookie("connectionString");
cookie.Value = value;
cookie.Expires = DateTime.Now.AddYears(100);
HttpContext.Current.Request.Cookies.Add(cookie);
}
}
private static string GetConnectionStringFromName(HttpCookie myCookie)
{
string connectionStringName = myCookie.Value;
return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}
private static void ExpireCookies(HttpContext current)
{
var allCookies = current.Request.Cookies.AllKeys;
foreach (var cook in allCookies.Select(c => current.Response.Cookies[c]).Where(cook => cook != null))
{
cook.Value = "";
cook.Expires = DateTime.Now.AddDays(-1);
current.Request.Cookies.Remove(cook.Name);
cook.Name = "";
}
}
}
现在,这似乎在登录后第一次单击时起作用。当我输入用户信息并逐步完成该过程时,我可以看到设置的 cookie/但是在我使用 PortalDataContext db = new PortalDataContext(AuthenticatedUser.ConnectionString); 的第一个真正的数据库调用中我收到空引用错误。它正在通过 Get 并检查 cookie 并返回一个空 cookie。这里可能发生了什么?
Scenerio - I am writting a web app that based off of which login box you use (database1 and database2) it will connect you to the cooresponding database. I have set the web.config to hold both connection strings. On the On_Authenticate method of each box I check to authenticate the user and then send a string based on the login box to a public variable in a class called Authenticate user. This public variable checks for the cookie and gets the connection string name from the variable, if the cookie is null, it sets a new cookie, expires all existing cookies and sets the cookie values.
Now once that is done, everytime I hit the database (using LINQ2SQL) I create a datacontext and send in AuthenticatedUser.ConnectionString. This will check the cookie and send get the connection string to use to pull the data. Herer is the AuthenticatedUser Class...
public class AuthenticatedUser : System.Web.UI.Page
{
public static string ConnectionString
{
get
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
return GetConnectionStringFromName(myCookie);
}
set
{
if (HttpContext.Current.Request.Cookies["connectionString"] != null)
{
ExpireCookies(HttpContext.Current);
}
var allCookies = HttpContext.Current.Request.Cookies.AllKeys;
HttpCookie cookie = new HttpCookie("connectionString");
cookie.Value = value;
cookie.Expires = DateTime.Now.AddYears(100);
HttpContext.Current.Request.Cookies.Add(cookie);
}
}
private static string GetConnectionStringFromName(HttpCookie myCookie)
{
string connectionStringName = myCookie.Value;
return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}
private static void ExpireCookies(HttpContext current)
{
var allCookies = current.Request.Cookies.AllKeys;
foreach (var cook in allCookies.Select(c => current.Response.Cookies[c]).Where(cook => cook != null))
{
cook.Value = "";
cook.Expires = DateTime.Now.AddDays(-1);
current.Request.Cookies.Remove(cook.Name);
cook.Name = "";
}
}
}
Now this seems to be working on the first click from the login. When I type in the user information and I step through the process I can see the cookie that is set/ However on the first true database call where I use PortalDataContext db = new PortalDataContext(AuthenticatedUser.ConnectionString);
I am getting a null reference error. It is going through the Get and it checks the cookie and returns a null cookie. What could be going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置cookie时需要将cookie添加到Reponse.Cookies集合中,而不是Request.Cookies集合中。仅响应中的 cookie 会发送回用户的浏览器。
You need to add the cookie to the Reponse.Cookies collection when setting it instead of the Request.Cookies collection. Only the cookies in the Response are sent back to the user's browser.