Have you defined a call back function once user is logged in? Ex- myMsalObj.handleRedirectCallback(callback);
A session is defined as the period of time that a unique user interacts with a Web application. Active Server Pages (ASP) developers who wish to retain data for unique user sessions can use an intrinsic feature known as session state.
Programmatically, session state is nothing more than memory in the shape of a dictionary or hash table, e.g. key-value pairs, which can be set and read for the duration of a user's session.
Classical ASP Session State Problems

ASP developers know that session state is a great feature, but one that is somewhat limited. These limitations include:

These are several of the problem sets that were taken into consideration in the design of ASP.NET session state.

ASP.NET 1.0 Session State

ASP.NET session state solves all of the preceding problems associated with classic ASP session state:

So, with the release of ASP.NET we got the following important session state options: "in-process mode", "out-of-process mode", "Cookieless" and "SQL Server mode". Let's look at them.

In-Process Mode

In-process mode simply means using ASP.NET session state in a similar manner to classic ASP session state. That is, session state is managed in-process and if the process is recycled, the state is lost. If we call SessionState.aspx, set a session state value, and stop and start the ASP.NET process (iisreset), the value set before the process was cycled will be lost. In-process mode is the default setting for ASP.NET.

How to configure it?

Out-of-process Mode

Included with the .NET SDK is a Windows NT service: ASPState. This Windows service is what ASP.NET uses for out-of-process session state management. To use this state manager, you first need to start the service.

How to configure it?

Cookieless State

We can configure the ASP.NET session state for a cookieless session state. Essentially this feature allows sites whose clients choose not to use cookies to take advantage of ASP.NET session state. This is done by modifying the URL with an ID that uniquely identifies the session:

http://localhost/(lit5py65t21z5v45vlm29s52)/Application/Products.aspx

To learn about sessions with and without cookies watch the nice video by questpond.com, here:

SQL Server Mode

The SQL Server mode option is similar to that of the Windows NT Service, except that the information persists to SQL Server rather than being stored in memory.

To use SQL Server as our session state store, we first must create the necessary tables and stored procedures that ASP.NET will look for on the identified SQL Server. The .NET SDK provides us with a SQL script file that we will execute on SQL Server to setup the database tables and stored procedures and then we will use the database credentials in ASP.NET Applications to start using SQL Server to manage the session states.

Why SQL Server Mode?

Once you start running multiple web servers for the same web site, the default ASP.Net session state ("InProc") will no longer be useful because you cannot guarantee that each page request goes to the same server. It becomes necessary to have a central state store that every web server accesses. SQL Server has a feature that offers you centralized storage of a session state in a Web farm. You can use SQL Server to save a session.

SQL Server Mode Advantages

Storing session variables in the SQL Server has the following advantages:

The session state mode can be configured via a <sessionState> tag of the web.config file.

Now, this step-by-step article demonstrates how to configure Microsoft SQL Server for ASP.NET SQL Server mode session state management.

Job 1: Configuring SQL Server to use ASP.NET's SQL Server Session State

Step 1: Find the SQL script file installed by .NET SDK and execute it on the SQL Server to setup the database.


Step 2: Double-click the above file to install it on the SQL Server. After installation you will get the following database tables and stored procedures:

Now we are done with the database setup. Let's create a demo web application and create a shopping cart like application that allows the user to add products to the cart and at the end will show the products list to the user. Think, if are developing an e-Commerce website that is using multiple servers, then how will you manage the sessions, because the session directly depends on the server and your website uses multiple servers, in this case you will lose all the sessions/products that the user selected when transferred to another server. No worries; we are using a centralized server that is SQL Server to manage our sessions. Go ahead and setup a website.

Job 2: Setup Web Application

At the very beginning, let's modify our existing web.config file to use SQL Server Mode Sessions. To do this add a "connectionstring" that will point to the "tempdb" database, as in:

  1. <connectionStrings>
  2. <add name="tempdbConnectionString1" connectionString="Data Source=ITORIAN-PC1;Initial Catalog=tempdb;Integrated Security=True"
  3. providerName="System.Data.SqlClient" />
  4. </connectionStrings>

And then, modify the <sessionState> section so that it looks like:

  1. <sessionState mode="SQLServer" customProvider="DefaultSessionProvider">
  2. <providers>
  3. <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="tempdbConnectionString1" />
  4. </providers>
  5. </sessionState>

You can notice the "mode" attribute in the above code that is using "SQLServer". Once you are done, let's set up some website pages.

Case Study: We will create two pages in our website, one will show the product list and another will show the selected products. We will call those pages by the names "Products.aspx" and "Cart.aspx". I'll be using the Northwind database in this project.

Products.aspx Code

  1. <div>
  2. <asp:GridView ID="GridView1" runat="server"
  3. AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
  4. Width="48%" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
  5. PageSize="5">
  6. <Columns>
  7. <asp:BoundField DataField="ProductName"
  8. HeaderText="ProductName"
  9. SortExpression="ProductName" />
  10. <asp:BoundField DataField="UnitPrice"
  11. HeaderText="UnitPrice"
  12. SortExpression="UnitPrice" />
  13. <asp:CommandField SelectText="Add to cart"
  14. ShowSelectButton="True" />
  15. </Columns>
  16. </asp:GridView>
  17. <asp:HyperLink ID="HyperLink1" runat="server"
  18. NavigateUrl="~/Cart.aspx" Font-Bold="True"
  19. Font-Size="Large">I'm Done, show products</asp:HyperLink>
  20. <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  21. ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"
  22. SelectCommand="SELECT [ProductName],
  23. [UnitPrice] FROM [Products]
  24. ORDER BY [ProductName]"></asp:SqlDataSource>
  25. </div>

Products.aspx.cs

  1. protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. DataSet ds = null;
  4. if (Session["sCart"] == null)
  5. {
  6. ds = new DataSet();
  7. DataTable dt = new DataTable();
  8. dt.Columns.Add(new DataColumn("ProductName"));
  9. dt.Columns.Add(new DataColumn("Qty", typeof(System.Int32)));
  10. ds.Tables.Add(dt);
  11. Session["sCart"] = ds;
  12. }
  13. else
  14. {
  15. ds = (DataSet)Session["sCart"];
  16. }
  17. DataRow row = ds.Tables[0].NewRow();
  18. row["productname"] = GridView1.Rows[GridView1.SelectedIndex].
  19. Cells[0].Text;
  20. row["Qty"] = 1;
  21. ds.Tables[0].Rows.Add(row);
  22. }

Cart.aspx Code

  1. <div>
  2. <asp:GridView ID="GridView1" runat="server"
  3. AutoGenerateColumns="False" Width="48%">
  4. <Columns>
  5. <asp:BoundField DataField="productname"
  6. HeaderText="Product Name" />
  7. <asp:BoundField DataField="qty"
  8. HeaderText="Quantity" />
  9. </Columns>
  10. </asp:GridView>
  11. </div>

Cart.aspx.cs Code

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. GridView1.DataSource = (DataSet)Session["sCart"];
  4. GridView1.DataBind();
  5. }

Now, you looked at some practical uses of the title.

Disadvantages of Storing the Session State in SQL Server

Though storing the session state in SQL Server makes your Web site more scalable and reliable, it has some disadvantages of its own:

Most of the theory resources in this article is taken from the MSDN.

I hope you like it. Thanks.