[英]ASP.NET Core 2.2 User.Identity has a value in razor but not in controller
I have an asp.net core 2.2 mvc project setup with windows authentication, my razor layout has this @User.Identity.Name which works great, but in my controller, User.Identity is null! 我有一个带有Windows身份验证的asp.net核心2.2 mvc项目设置,我的剃刀布局具有此@ User.Identity.Name,该效果很好,但在我的控制器中,User.Identity为null! any idea what I am doing wrong?
知道我在做什么错吗?
here's a sample of my controller: 这是我的控制器的示例:
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using UI.Models;
namespace UI.Controllers
{
public class HomeController : Controller
{
string shortName = string.Empty;
public HomeController()
{
shortName = User.Identity.Name;
}
public IActionResult Index()
{
return View();
}
}
}
You cannot access User
, HttpContext
, or other related properties within the constructor of a controller. 您不能在控制器的构造函数中访问
User
, HttpContext
或其他相关属性。 That means that you can access these properties only within an actual controller action , like your Index
method. 这意味着您只能在实际的控制器操作 (例如
Index
方法)内访问这些属性。
To understand this, you need to understand how these controller properties actually work and how the framework will manage the lifetime of a controller. 要了解这一点,您需要了解这些控制器属性实际上是如何工作的,以及框架将如何管理控制器的生存期。 As you may be aware, controllers are short-lived objects that are created temporarily only for the duration of a single request.
如您所知,控制器是短暂的对象,仅在单个请求期间才临时创建。 That means that they will always execute within the scope of a single request.
这意味着它们将始终在单个请求的范围内执行。
By that logic, accessing the user or other properties of the HTTP context should work just fine; 按照这种逻辑,访问用户或HTTP上下文的其他属性应该可以正常工作。 and it usually is.
通常是这样 However, when you inherit those properties from
Controller
or ControllerBase
, then those don't magically have a value. 但是,当您从
Controller
或ControllerBase
继承这些属性时,这些属性就不会神奇地具有值。 Instead, they will be set explicitly by the framework after it creates the controller. 相反,它们将在框架创建控制器后由框架显式设置。
The logic for this is simlar to this: 这样做的逻辑与此类似:
// create the controller with its dependencies
var controller = CreateController<HomeController>();
// set the controller context
controller.ControllerContext = context;
// invoke action
controller.Index();
So when the object is constructed and the constructor runs, you can access anything that is a direct dependency of the controller, since the dependency injection container will provide all those values through constructor injection. 因此,在构造对象并构造函数运行时,您可以访问任何与控制器直接相关的内容,因为依赖性注入容器将通过构造函数注入提供所有这些值。
Anything else though will not be available automatically. 但是,其他所有内容将不会自动提供。 That includes all the default properties that you inherit from
Controller
or ControllerBase
. 这包括您从
Controller
或ControllerBase
继承的所有默认属性。 These are usually implicitly set by the ControllerContext
, so you will have to wait for the framework to set those. 这些通常是由
ControllerContext
隐式设置的,因此您将不得不等待框架设置它们。
Since it is impossible to set a property value before the constructor runs, you will simply not be able to access any of these values within the constructor. 由于无法在构造函数运行之前设置属性值,因此您将根本无法访问构造函数中的任何这些值。 So the solution is to move the logic into an action instead.
因此,解决方案是将逻辑转变为动作。 That way, you can access the properties and they will have the proper values.
这样,您可以访问属性,它们将具有正确的值。
public class HomeController : Controller
{
public IActionResult Index()
{
var shortName = User.Identity.Name;
return View();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.