简体   繁体   中英

C# Cookies based on login information

I have 2 login controls on a web application one on default and one on default2 (the naming convention will be updated after I get it working).

What I am doing is setting a cookie on each login that will send a connectionstring name from the login controls authenticate method. It is sending a string that is hard coded to a base class called Authenticate Users. The class is doing the following...

public class AuthenticatedUser : System.Web.UI.Page
{
public static string ConnectionString
{
    get
    {
        HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
        return GetConnectionStringFromName(myCookie);
    }
    set
    {
        HttpCookie oldCookie = HttpContext.Current.Request.Cookies["connectionString"];
        oldCookie.Expires = DateTime.Now.AddDays(-1);
        HttpCookie cookie = new HttpCookie("connectionString");
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Request.Cookies.Add(cookie);
        string val = cookie.Value;
    }
}

private static string GetConnectionStringFromName(HttpCookie myCookie)
{
    string connectionStringName = myCookie.Value;
    return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}

}

I am seding in the strings "database1" and "database2" depending on which login control they use. When I debug through the code, the connectionstring is setting all of the cookie information and everything works great for "database2" however everytime I log in using the form associated with "database1" it sets the cookie but when the get is called it is still referencing "database2"

Is this an issue because the cookies are named the same and do not overwrite eachother or update themselves or is there a problem with my code?

edit -- it is still not working with removing the cookies from the context, it still gives me the "database2" when I run "database1"

 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);
        }
        HttpCookie cookie = HttpContext.Current.Response.Cookies["connectionString"];
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Response.Cookies.Add(cookie);
        string val = cookie.Value;
    }
}

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.Response.Cookies.Remove(cook.Name);
    }
} 

Final Edit It works...here is the working code if anyone is interested...

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);
        string val = cookie.Value;
    }
}

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 = "";
    }
} 

you have part of the code needed (expiring the cookie). but you also need to remove from the context, try something like the following:

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.Response.Cookies.Remove(cook.Name);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM