HttpContext.Items["Properties"]全局获取

2020-12-03 14:41发布

问题:

services.AddAuthentication("auth")
.AddCookie("auth",config=>
{
config.Cookie.Name = "cookie.name";
//config.LoginPath = "/home/login";
config.Events.OnRedirectToLogin = context =>
{
context.HttpContext.Items["Properties"] = context.Properties;
context.HttpContext.Features.Set(context.Properties);
var get=HttpContext.Session.Get("Properties");
return Task.CompletedTask;
};
});

如何在一个action请求里获取 Properties。HttpContext.Items["Properties"]的作用域仅限于一次请求,但我想在某个action中获取。

public IActionResult actionmethod()
{
var property = HttpContext.Items["Properties"];
var pro = HttpContext.Features.Get<AuthenticationProperties>();
var get=HttpContext.Session.Get("Properties");
return View();
}
能不能实现,或者有没有替代方法?

回答1:

文档上说明是有吧 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0