ASP.NET控制器基类User.Identity.Name [英] ASP.NET Controller Base Class User.Identity.Name
本文介绍了ASP.NET控制器基类User.Identity.Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如这篇文章中所述,我创建了一个抽象基本控制器类,以便能够将数据从控制器传递到master.page。在这种情况下,我想查找一个用户在我的数据库,查询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. In this case, I want to lookup a user in my db, querying for User.Identity.Name (only if he is logged in).
但是,我注意到,在这个抽象基类中, User
属性总是 null
。
However, I noticed that in this abstract base class the User
property is always null
. What do I have to do to get this working?
感谢很多
推荐答案
尝试覆盖Controller.Initialize()而不是:
Try overriding Controller.Initialize() instead:
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;
}
}
这篇关于ASP.NET控制器基类User.Identity.Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文