我到处都是网络,以找出我使用HttpContext.Session出了什么问题。似乎在设置会话时已在设置会话,但是在离开函数后,我无法访问它。我已经检查了Startup.cs文件,以确保正确添加了所有内容并安装了正确的NuGet软件包。
在我实际使用会话的控制器类中,我有以下代码:
/// <summary>
/// Check to see if user exists. If yes, go to the administrator interface,
/// else return to the login page.
/// </summary>
/// <param name="administrator"></param>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AdminLogin([Bind("UserName", "Password")] Administrator administrator)
{
try
{
var admin = _context.Administrators.Where(x => x.UserName == administrator.UserName
&& x.Password == administrator.Password).FirstOrDefault();
if (admin != null)
{
HttpContext.Session.SetString("IsAdmin", "true");
_httpContextAccessor.HttpContext.Response.Cookies.Append("IsAdmin", "true");
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Shows the administrator interface
/// </summary>
public IActionResult Index()
{
if (HttpContext.Session.GetString("IsAdmin") == "true"
|| _httpContextAccessor.HttpContext.Request.Cookies["IsAdmin"] == "true")
{
ViewData["isAdmin"] = "true";
HttpContext.Session.SetString("IsAdmin", "true");
ViewData["Title"] = "Administrator Page";
ViewData["Message"] = "Administrator Page";
return View();
}
else
{
return RedirectToAction("AdminLogin");
}
}
如您所见,我正在AdminLogin函数中设置会话变量(IsAdmin),然后尝试在“索引”视图中访问该会话。我有ValidateAntiForgeryToken,我认为这可能是我的问题,但是即使删除它,我的会话也不会持久。我也尝试使用cookie作为一种解决方法,但是它们也不持久。如果有人愿意帮助我,我可以发布更多代码。如果无法正常工作,则必须重新开始并且不使用Asp.NET Core。我的应用程序的其余部分最终也将不得不使用会话。
这是我的startup.cs,只是为了确保它确实设置正确...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OM2018a.Data;
using Microsoft.EntityFrameworkCore;
namespace OM2018a
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<OM2018Context>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// Add MVC services to the services container.
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(15);
});
services.AddHttpContextAccessor();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
// IMPORTANT: This session call MUST go before UseMvc()
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
答案 0 :(得分:4)
我找到了答案! Session variable value is always getting null in ASP.NET Core 2.1
我不得不更改选项。CheckConsentNeeded = context => true;为假,它奏效了!
答案 1 :(得分:1)
您必须将Startup.cs文件options.CheckConsentNeeded = context => true;
更改为options.CheckConsentNeeded = context => false;
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
.....
}
就是这样。