Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:52:34+00:00 2026-05-17T01:52:34+00:00

Scenerio – I am writting a web app that based off of which login

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:52:35+00:00Added an answer on May 17, 2026 at 1:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope you all will be fine. The scenerio is that i have a page
The scenerio is like this: I have to use multi-field query, which indexed several
Please consider such scenerio: I have component called TMenuItemSelector which has two published properties:
Hi i have a scenerio i have a database dump which i want to
I am looking for a real world scenerio where using exec will be the
How does one code this scenerio in iphone sdk? In an expense app, when
Hope you all will be fine. The scenerio is I have a form on
I came around a interesting issue while working on my app. Imagine the scenerio
Here's the scenerio. I have a report that is grabbing daily counts from the
I have working Hibernate/Spring web-app ( HibernateDaoSupport , getHibernateTemplate() , etc). For several tasks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.