在控制器构造函数中定义用户与User.Identity.Name [英] Defining a User with User.Identity.Name in controller constructor

查看:114
本文介绍了在控制器构造函数中定义用户与User.Identity.Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的行动是要与用户的账户进行互动,我想创造另外一个TheUser对象并称对象为计算机[TheUser]只要我的控制器上的任何动作调用。

如果用户登录,会抢在用户信息从数据库中,如果没有,TheUser的对象也只是空。

我想在控制器的构造函数访问User.Identity.Name,但它是不是之前被调用的任何行动创建。

我是看着自定义授权过滤器,但这些不会让我创造的TheUser对象并将其存储在ViewData的。

这是我想完成的简短片段:

  [授权]
公共类HomeController的:控制器
{
    用户TheUser;    公共HomeController的()
    {
        TheUser = User.Identity.IsAuthenticated? UserRepository.GetUser(User.Identity.Name):空;        计算机[TheUser] = TheUser;
    }
}


解决方案

我创建了一个自定义的基础类,并增加了的currentUser的属性吧。

在初始化方法中,我置于逻辑来获取用户,然后将它添加到的ViewData和控制器的的currentUser属性。

我有我的控制器继承自基础类,我能够在任何地方控制参考的currentUser变量:

 公共类CustomControllerClass:控制器
{
    公众用户的currentUser;    保护覆盖无效初始化(System.Web.Routing.RequestContext的RequestContext)
    {
        base.Initialize(RequestContext的);        如果(requestContext.HttpContext.User.Identity.IsAuthenticated)
        {
            字符串username = requestContext.HttpContext.User.Identity.Name;
            的currentUser = UserRepository.GetUser(用户名);
            计算机[的currentUser] =的currentUser;
        }
        其他
            计算机[的currentUser] = NULL;
    }
}

For my actions that are going to interact with the User's account, I would like to create a "TheUser" object in addition to adding that object to "ViewData["TheUser"]" as soon as any action on my controller is called.

If the User is logged in, it will grab the User's info from the database, if not, "TheUser" object will just be null.

I tried accessing "User.Identity.Name" in the controller constructor, but it isn't created prior to any action being called.

I was looking at custom authorization filters, but those wouldn't allow me to create the "TheUser" object and store it in the ViewData.

This is a brief snippet of what I would like to accomplish:

[Authorize]
public class HomeController : Controller
{
    User TheUser;

    public HomeController()
    {
        TheUser = User.Identity.IsAuthenticated ? UserRepository.GetUser(User.Identity.Name) : null;

        ViewData["TheUser"] = TheUser;  
    }
}

解决方案

I created a custom base Controller class and added a "CurrentUser" property to it.

In the Initialize method, I placed the logic to get the user and then add it to the ViewData and the "CurrentUser" property of the controller.

I had my controllers inherit the custom base Controller class and I was able to reference "CurrentUser" variable anywhere in the controller:

public class CustomControllerClass : Controller
{
    public User CurrentUser;

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

        if (requestContext.HttpContext.User.Identity.IsAuthenticated)
        {
            string userName = requestContext.HttpContext.User.Identity.Name;
            CurrentUser = UserRepository.GetUser(userName);
            ViewData["CurrentUser"] = CurrentUser;
        }
        else
            ViewData["CurrentUser"] = null;
    }
}

这篇关于在控制器构造函数中定义用户与User.Identity.Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆