views:

32

answers:

1

I'm working on a web application that is using Forms authentication.

    <authentication mode="Forms">
        <forms slidingExpiration="true"
         loginUrl="~/User.aspx/LogOn"
         timeout="15"
         name="authToken"  />
    </authentication>

I'm seeing this cookie set in my browser when I log in:

alt text

The question is what happens when I put this website in a load balanced model? Where is the ASP.net session cookie being set? I didn't explicitly do it in code, so I assume it's happening behind the scenes somewhere in ASP.Net.

Also, If the session cookie is set by web server A, I assume web server B won't recognize it and treat it as an invalid session. If this is the case, I probably don't want to use it, right?

+7  A: 

Youll have to set the machine key to be the same and the name to be the same on both machines...if this is done you should have no problems load balancing with forms auth.

        <authentication mode="Forms">
        <forms loginUrl="~/Login/Index" defaultUrl="~/"
                     name=".myportal"
                     protection="All" slidingExpiration="true" timeout="20" path="/"
                     requireSSL="false"></forms>
    </authentication>

    <machineKey validationKey="534766AC57A2A2F6A71E6F0757A6DFF55526F7D30A467A5CDE102D0B50E0B58D613C12E27E7E778D137058E" decryptionKey="7059303602C4B0B3459A20F9CB631" decryption="Auto" validation="SHA1"/>

Sessions can get slightly more complicated. You can store the ASP.Net session state in the database or use a shared session provider to make it available for load balancing as well.

Here is a good article on storing session state in the DB: http://idunno.org/articles/277.aspx

Climber104
You also need to make sure you're using a session state provider that works across machines (i.e. *not* `mode="inproc"`), but other than that, yeah it works just fine.
Dean Harding
@Climber104 Ahh... I see, so the session cookie must be something like an encrypted timestamp. I assume the machine key is used to do encryption/decryption and that's why it needs to be the same.
Paul Fryer
If you are load balancing, you can not use Session of mode="InProc". Not even with sticky sessions. You need to store it in IIS, a database, or create your own provider. I've implemented storing the session in IIS and it works just fine.
TheGeekYouNeed
Exactly. In order to decrypt the auth token, you need to share the validation key.
Climber104
So to change to sql server, is it as simple as:1) Change from InProc to SQL Server in IIS2) Set connection string in IIS (obviously install DB if needed)
Paul Fryer