【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" }));
}
...
}
}
}
推荐阅读
- 控制器未加载.AspNetCore.Identity.Application用户cookie
- org.hibernate.MappingException(未知实体:com.app.persistence.Test)
- 修改/删除现有的inapp订阅产品
- 我可以在谷歌玩游戏中使用相同的排行榜和成就安卓游戏
- Android - 从CH Play打开时恢复应用程序
- android面临崩溃报告仅适用于8.0和8.1中的firebase通知
- 在Play商店中推送特定版本的android的更新
- 实施InApp购买
- 如何在设置Google Merchant帐户后将Android应用程序更改为付费()