简体   繁体   English

ASP.NET Core 2.2 User.Identity在剃刀中有一个值,但在控制器中没有

[英]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. 您不能在控制器的构造函数中访问UserHttpContext或其他相关属性。 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. 但是,当您从ControllerControllerBase继承这些属性时,这些属性就不会神奇地具有值。 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 . 这包括您从ControllerControllerBase继承的所有默认属性。 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.

相关问题 控制器构造函数中的 ASP.NET Core 访问 User.Identity - ASP.NET Core Access User.Identity in Controller Constructor Razor 对 asp.net 核心 2.2 中的 Controller 的页面表单操作 - Razor Page form action to Controller in asp.net core 2.2 将 JWT 自动加载到 User.Identity (ASP.NET Core 3.1) - Load JWT into User.Identity automatically (ASP.NET Core 3.1) 单元测试依赖于User.Identity对象的ASP.Net Web API控制器 - Unit Testing an ASP.Net Web API Controller that relies on the User.Identity object ASP.NET WebApi User.Identity返回null - ASP.NET WebApi User.Identity returns null 如何扩展asp.net User.Identity属性? - How to extend the asp.net User.Identity Property? Asp.Net Core 2.2身份验证 - Asp.Net Core 2.2 Identity Authentication Asp.Net Core Identity 2.2和Identity Server 4,更改用户ID类型会导致数据库上下文错误 - Asp.Net Core Identity 2.2 and Identity Server 4, Changing User Id type results in an error in the db context 如何向 ASP.NET MVC user.Identity 添加更多用户数据 - How to add more user data to ASP.NET MVC user.Identity asp.net core 2.2 razor 页面登录不持久。 很快用户必须重新登录 - asp.net core 2.2 razor page login not persistent. very soon user must re login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM