asp.netiiscookiessubdomainapplication-pool

How can I create and test two web apps in two separate application pools on my local machine?


I have been tasked with determining how to maintain user sessions via cookies from one subdomain to another across multiple application pools. My findings so far indicate that subdomains can share cookies across pools, but I need to be able to demo this live.

I have never worked with application pools before, and I just want to have one instance of my simple cookie baking/reading web app running on pool A at localhost:3000 and another instance running on pool B at localhost:3001. Many of the answers I've found just say "create the application pool and add your app but I have trouble finding details on how to do this specifically.

The web app I'm running is this example from HttpCookieCollection.


Solution

  • You could follow these below steps:

    1. Open iis manager

    2. Create 2 websites with the below configuration:

    Set the folder path site name and ip address based on your test .ip address you can select from the dropdown and choose the folder path as your website publish folder.

    enter image description here

    Repeat same steps and create second website.

    1. If you doing local test and would like to use test domain you could use the host file which is located at C:\Windows\System32\drivers\etc and add your test domain in that file with your machine ip address

    Add the entries as shown below instead of 127.0.0.1 use your machine ip

    enter image description here

    To share the cookie you could use thie below code:

    protected void SetCookieButton_Click(object sender, EventArgs e)
    {
        // Create and set cookie
        HttpCookie cookie = new HttpCookie("MySharedCookie", "Hello from AppA");
       cookie.Domain = ".domain.com"; // Sharing across subdomains
        cookie.Path = "/";
        cookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(cookie);
    
        // Refresh page to show the cookie value
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    

    Or use this code in web.config file:

     <system.web>
           <httpCookies domain=".domain.com" />
         </system.web>
    

    Test Result:

    enter image description here

    enter image description here