Difference between revisions of "Setup Session for ASP.Net Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Setting up Session for ASP.Net Core Web App= Using the nuget package manager you need to install `Microsoft.AspNetCore.Session`. The version I have used is 2.2.0. You can no...")
 
(Inside the PageModel)
 
(9 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
options.Cookie.IsEssential = true;
 
options.Cookie.IsEssential = true;
 
});
 
});
 +
</syntaxhighlight>
 +
 +
This code must be before the line `var app = builder.Build();` but after the line `var builder = WebApplication.CreateBuilder(args);`.
 +
 +
You now must add this:
 +
<syntaxhighlight lang=c#>
 +
app.UseSession();
 +
</syntaxhighlight>
 +
 +
It should go before your `app.Run()`.
 +
 +
==Setting a Session Variable==
 +
The session can be accessed using `HtpContext`, but only in the methods called `OnGet()` or `OnPost()`. If you try to access session anywhere else it will be null. So remember `OnGet()` is run when the page is loaded and `OnPost()` is run when the page is submitted using a form.
 +
 +
<syntaxhighlight lang=c#>
 +
    HttpContext.Session.SetString("test", Request.Form["test"]);
 +
</syntaxhighlight>
 +
 +
==Retrieving a Session Variable==
 +
===Inside a Razor Page===
 +
<syntaxhighlight lang=c#>
 +
@HttpContext.Session.GetString("test")
 +
</syntaxhighlight>
 +
 +
===Inside the PageModel===
 +
<syntaxhighlight lang=c#>
 +
public void OnGet()
 +
{
 +
        if (string.IsNullOrEmpty(HttpContext.Session.GetString("SomeSessionVariable")))
 +
        {
 +
            HttpContext.Session.SetString("test", "test");
 +
            HttpContext.Session.SetInt32("test1", 73);
 +
        }
 +
}
 +
</syntaxhighlight>
 +
 +
==Removing a Session Variable==
 +
You can remove an individual session variable:
 +
<syntaxhighlight lang=c#>
 +
HttpContext.Session.Remove("test");
 +
</syntaxhighlight>
 +
 +
==Destroy a Session==
 +
You can destroy a session using the `CLear()` command. This will remove all session variables from the session.
 +
<syntaxhighlight lang=c#>
 +
HttpContext.Session.Clear();
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 08:05, 23 October 2024

Setting up Session for ASP.Net Core Web App

Using the nuget package manager you need to install `Microsoft.AspNetCore.Session`. The version I have used is 2.2.0.

You can now add the following code to your `program.cs` file:

builder.Services.AddDistributedMemoryCache();

builder.Services.AddSession(options =>
{
	options.IdleTimeout = TimeSpan.FromSeconds(10);
	options.Cookie.HttpOnly = true;
	options.Cookie.IsEssential = true;
});

This code must be before the line `var app = builder.Build();` but after the line `var builder = WebApplication.CreateBuilder(args);`.

You now must add this:

app.UseSession();

It should go before your `app.Run()`.

Setting a Session Variable

The session can be accessed using `HtpContext`, but only in the methods called `OnGet()` or `OnPost()`. If you try to access session anywhere else it will be null. So remember `OnGet()` is run when the page is loaded and `OnPost()` is run when the page is submitted using a form.

     HttpContext.Session.SetString("test", Request.Form["test"]);

Retrieving a Session Variable

Inside a Razor Page

@HttpContext.Session.GetString("test")

Inside the PageModel

public void OnGet()
{
        if (string.IsNullOrEmpty(HttpContext.Session.GetString("SomeSessionVariable")))
        {
            HttpContext.Session.SetString("test", "test");
            HttpContext.Session.SetInt32("test1", 73);
        }
}

Removing a Session Variable

You can remove an individual session variable:

HttpContext.Session.Remove("test");

Destroy a Session

You can destroy a session using the `CLear()` command. This will remove all session variables from the session.

HttpContext.Session.Clear();