[英]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: 因此,我有两个问题:
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.