代码之家  ›  专栏  ›  技术社区  ›  ffejrekaburb

启动类是IsInRole。网芯

  •  0
  • ffejrekaburb  · 技术社区  · 7 年前

    我在创业班的ConfigureServices里工作。我想运行IsInRole(),我理解它是这个方法,以便填充操作:

    然而,跑步:

    System.Security.Principal.IPrincipal.IsInRole("BRV_Projects_Edit");
    

    显然是不成功的,因为这指的是一个接口,而不是实现该接口的实例?

    我的问题是,在ConfigureService的上下文中,我定义了一个自定义属性,如何访问(注入?)当前用户的主体。

    1 回复  |  直到 7 年前
        1
  •  0
  •   ffejrekaburb    7 年前

    根据以上用户反馈。InRole此时无法在管道中访问,但可以通过创建ActionFilterAttribute来访问。

    ActionFilterAttribute使ActionExecuting可用, Get User Name on Action Filter 非常类似于这篇文章,除了与。网芯

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.Controller is Controller controller)
            {
                controller.ViewBag.CanEdit = filterContext.HttpContext.User.IsInRole("group1");
    
    推荐文章