I have added all the thing but still not working
here is the startup class
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, opts => { opts.ResourcesPath = "Globalization"; })
.AddDataAnnotationsLocalization()
.AddSessionStateTempDataProvider();
services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.Name = ".Project.Session";
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
});
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddTransient<ICustomerTB1, CCustomerTB1>();
services.AddTransient<IUsmUser, CUsmUser>();
services.AddTransient<IUsmUserType, CUsmUserType>();
services.AddTransient<IUsmOffice, CUsmOffice>();
services.AddTransient<IUsmOfficeType, CUsmOfficeType>();
services.AddTransient<ISycGender, CSycGender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseStatusCodePages();
app.UseAuthentication();
app.UseSession();
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller=Account}/{action=Login}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Index}/{id?}"
);
});
}
}
What I have tried:
and on controller i have done
HttpContext.Session.SetString("Name", "aadfafdaf");
var name = HttpContext.Session.GetString("Name");
if i select
var name = HttpContext.Session.GetString("Name");
on same controller after adding on session it works.
But on other controller action it returns null.