Monday, June 3, 2024

ASP.Net core Interview Questions

 

How to use sessions in Asp.net Core?

1.      Install the Microsoft.AspNetCore.Session NuGet Package

2.      Configure Session Services inStartup.cs

3.      public void configureServices(IServiceConfigurationServices);

{

 services.AddDistributedMemoryCache(); // For in-memory session storage

services.AddSession( option =>

{

  Option.IdleTimeout = TimeSpan.FromMinutes(30);

  Option.cookie.HttpOnly = true;

  Option.cokiee.ISessitial = true;

}

 

)

Inject the ISessionInterface:

public class HomeController: Controller

Public readonly IHttpContextAccessor _httpContextAccessor

public HomeController(IHttpContextAccessor   httpContextAccessor);

{

_httpContextAccessor = httpContextAccessor;

}

httpContextAccessor. HttpContext.Session.SetString( "username" , username);

var username = _httpContextAccessor.HttpContext.Session.GetString( "username" );

No comments:

Post a Comment

ASP.Net core Interview Questions

  How to secure web Api Application in core: ·         Add Authentication and Authorization ·         Secure communication with TLS ...