在控制器构造函数中定义用户与User.Identity.Name

编程入门 行业动态 更新时间:2024-10-23 11:19:30
本文介绍了在控制器构造函数中定义用户与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

本文发布于:2023-11-17 04:59:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   函数   定义   用户   Identity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!