views:

8

answers:

1

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?

+1  A: 

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.

tvanfosson
What I am stroring is the connectionstring name not the actual connection string. I send that name into GetConnectionStringFromName() to get the connection string itself
EvanGWatkins
@Evan -- sorry, I missed that. So, then, all you need to do is store the cookie in the Response instead.
tvanfosson
Thanks I cant believe I over looked that. Thanks!
EvanGWatkins