ApplicationUser在Asp.Net Core 2.0中的ActionFilter中()

【ApplicationUser在Asp.Net Core 2.0中的ActionFilter中()】出门莫恨无人随,书中车马多如簇。这篇文章主要讲述ApplicationUser在Asp.Net Core 2.0中的ActionFilter中?相关的知识,希望能为你提供帮助。
如何在Asp.Net Core 2.0中的ApplicationUser中访问当前的UserManager(或ActionFilter)?
我试图锁定整个应用程序,直到用户接受EULA(最终用户许可协议),更改其密码并填写所需的个人信息。

public class ApplicationUser : IdentityUser { ... public DateTime? DateEULAAccepted { get; set; } ... }

这是ActionFilter代码:
public class ProfileRequiredActionFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.User.Identity.IsAuthenticated) { var CurUser = UserManager< ApplicationUser> .GetUserAsync(filterContext.HttpContext.User); ... if (CurUser.Result.DateEULAAccepted.ToString() == null) { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Account", action = "AgreeToEULA" })); } ... } } }

我在ActionFilter中实例化Startup > ConfigureServices如下:
... services.AddMvc(options => { options.Filters.Add(new ProfileRequiredActionFilter()); }); ...

答案尝试在ConfigureServices()中添加过滤器,如下所示:
services.AddMvc(options => { options.Filters.Add< ProfileRequiredActionFilter> (); });

然后,您可以将UserManager注入过滤器,如下所示:
public class ProfileRequiredActionFilter : IActionFilter { private UserManager< ApplicationUser> _userManager; public ProfileRequiredActionFilter(UserManager< ApplicationUser> userManager) { _userManager = userManager }public void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.User.Identity.IsAuthenticated) { var CurUser = _userManager< ApplicationUser> .GetUserAsync(filterContext.HttpContext.User); ... if (CurUser.Result.DateEULAAccepted.ToString() == null) { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Account", action = "AgreeToEULA" })); } ... } } }


    推荐阅读