Asp.net Razor Pages Session:
A session is used to store data in browser cached and store in a session variable and access to the whole environment of the application. but razor pages is different seen, it has a page level scope if you want to use session variables globally it is not suitable at this time.so I preferred to you used claim based authentication used cookies is simple and easy to use. so describe below deeply.
USes in Every.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json.Linq;
namespace QMPortal.Pages.ServiceProvider
{
[Authorize]
public class AddModel : PageModel
{
public void OnGet()
{
var SessionUser= HttpContext.User.Identity.Name;
ViewData["SessionUser"] = SessionUser;
}
}
}
and if you want to use in Html.cs file like used in view so syntexis
@{
var SessionUser= Context.User.Identity.Name;
var SessionUser = JObject.Parse(SessionUser);
ViewData["SessionUser"] = SessionUser;
}
that is simp-le tutorial to learn using session varible

0 Comments