简体   繁体   English

ASP.NET控制器基类User.Identity.Name

[英]ASP.NET Controller Base Class User.Identity.Name

As described in this post , I created an abstract base controller class in order to be able to pass data from a controller to master.page. 如在描述的这篇文章中,我创建一个抽象基类控制器,以便能够从控制器的数据传递到master.page。 In this case, I want to lookup a user in my db, querying for User.Identity.Name (only if he is logged in). 在这种情况下,我想在我的数据库中查找用户,查询User.Identity.Name(仅当他登录时)。

However, I noticed that in this abstract base class the User property is always null . 但是,我注意到在这个抽象基类中, User属性始终为null What do I have to do to get this working? 要做到这一点,我该怎么办?

Thanks a lot 非常感谢

As Paco suggested, the viewdata isn't initialized till after you are trying to use it. 正如Paco建议的那样,在您尝试使用它之前,viewdata尚未初始化。

Try overriding Controller.Initialize() instead: 尝试重写Controller.Initialize():

public abstract class ApplicationController : Controller
{
    private IUserRepository _repUser;

    public ApplicationController()
    {
    }

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);

        _repUser = RepositoryFactory.getUserRepository();
        var loggedInUser = _repUser.FindById(User.Identity.Name);
        ViewData["LoggedInUser"] = loggedInUser;
    }
}

要使用该用户,您应该从中获取当前页面

HttpContext.Current.User.Identity.Name

通过在web.config中将身份验证设置为Windows,您可以使用User.Identity.Name获取用户

I use Page class on my static Utlilites classes. 我在静态Utlilites类上使用Page类。 Like that; 像那样;

Page P = (Page)HttpContext.Current.Handler; Page P =(页)HttpContext.Current.Handler;

and i can get all properties via the P object for the current requested page.. 我可以通过P对象获取当前请求页面的所有属性。

你试过这个:ControllerContext.HttpContext.Current.User.Identity.Name?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试使用User.Identity.Name的Asp.Net控制器操作方法? - Test an Asp.Net Controller action method which uses User.Identity.Name? 会话与User.Identity.Name进行比较,以使用Windows身份验证检查ASP.net核心应用程序的登录用户名 - Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication ASP.NET Web 表单应用程序 User.Identity.Name 在 IIS 中不起作用 - ASP.NET web form application User.Identity.Name not working in IIS User.Identity.Name 是 null 在我的 ASP.NET 核心 Web ZDB974238714CA8A14A7CE1D0 - User.Identity.Name is null in my ASP.NET Core Web API User.Identity.Name 在 asp.net core + angular 客户端应用程序中为空 - User.Identity.Name is null in asp.net core + angular client app 将参数值传递给SqlDataSource ASP.NET User.Identity.Name - Pass parameter value to SqlDataSource ASP.NET User.Identity.Name 为什么运行 ASP.NET 6 项目时 `User.Identity.Name` `null`? - Why is `User.Identity.Name` `null` when running ASP.NET 6 project? 如何在ASP.NET中使用User.Identity.Name作为SqlDataSource的参数? - How to use User.Identity.Name as a parameter for SqlDataSource in ASP.NET? User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null - User.Identity.Name returns null in ASP.NET Core MVC application 对于托管在 asp.net core 中的 Blazor Wasm,User.Identity.Name 为 null - User.Identity.Name is null for Blazor Wasm hosted in asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM