简体   繁体   English

从ASP.NET 5中的基本控制器构造函数访问身份用户

[英]Access Identity User from base controller constructor in ASP.NET 5

I tried to access the Identity current User in my abstract base controller constructor to use the link to display user in View if It signed in. 我尝试在抽象基本控制器构造函数中访问“标识当前用户”,以使用该链接在View登录后在View中显示用户。

So, I got User property is null . 所以,我得到User属性为null Everybody advice use filter and return User via ViewData. 每个人的建议都使用过滤器并通过ViewData返回用户。 Example here: "Why is User (as in User.Identity.Name) null in my abstract base controller?" 此处的示例: “为什么我的抽象基本控制器中的User(如User.Identity.Name)为null?”

I tried do somethink like this, but face with the problem: AuthorizationContext has no member Controller ! 我尝试做这样的事情,但是面对问题: AuthorizationContext 没有成员 Controller

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class UserAuthorizationAttribute : Attribute, IAuthorizationFilter
{
    public void OnAuthorization(Microsoft.AspNet.Mvc.Filters.AuthorizationContext filterContext)
    {
        filterContext.Controller.ViewData["IdentityUser"] = filterContext.HttpContext.User;
    }
}

So, I have two questions: 因此,我有两个问题:

  • Is there any way to send data from Filter to the Controller? 有什么方法可以将数据从过滤器发送到控制器?
  • Maybe there is any other way to get not null User? 也许还有其他方法来获取不为空的用户?

You could try using an Action Filter as opposed to an Authorisation Filter. 您可以尝试使用操作过滤器而不是授权过滤器。 These are processed later in the pipeline, and have access to the user. 这些将在管道中稍后处理,并可以访问用户。

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

相关问题 控制器构造函数中的 ASP.NET Core 访问 User.Identity - ASP.NET Core Access User.Identity in Controller Constructor ASP.NET控制器基类User.Identity.Name - ASP.NET Controller Base Class User.Identity.Name 从基础asp.net标识用户创建继承用户 - Creating inheritance users from base asp.net identity user 从ASP.NET Core Web API中的控制器访问用户标识 - Access user identity from controller in ASP.NET Core Web API 如何在Asp.Net Web API控制器构造函数中注入用户身份? - How to inject User Identity in Asp.Net web api controller constructor? 如何在 ASP.NET Core Identity 中控制器的构造函数中获取登录用户名 - How to get logged-in user name in the constructor of a controller in ASP.NET Core Identity ASP.NET核心标识中的基本控制器继承 - Base Controller inheritance in ASP.NET Core identity 使用Unity的ASP.NET MVC中的基本控制器构造函数注入 - Base controller constructor injection in ASP.NET MVC with Unity ASP.NET Core MVC:编辑用户,将数据从视图传递到 controller 时出现问题,身份 - ASP.NET Core MVC : edit user, problem with passing data from view to controller ,identity 在 MVC6 中使用 ASP.NET Identity 时从控制器访问登录用户的字段 - Accessing the logged in User's fields from the controller when using ASP.NET Identity with MVC6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM