我尝试从我的自定义actionfilter属性重定向到另一个操作,但设置filtercontext.result什么都不做

时间:2017-02-21 21:48:15

标签: c# asp.net-core .net-core

这是我的OnActionExecuting方法。我检查了Console.WriteLine条件是否满意,结果是。

编辑:我打电话后设置上下文结果似乎不起作用

user = await _userManager.GetUserAsync(context.HttpContext.User);

当我在

之前设置context.result时,

重定向有效

 [AttributeUsage(AttributeTargets.All ,Inherited = true)]
public class HasPermissionAttribute: ActionFilterAttribute
{
    private string _permission;
    private AdminDbContext _context;
    private UserManager<AdminUser> _userManager {get;set;}
    private AdminUser user {get;set;}

    private readonly IHostingEnvironment _hostingEnvironment;



    public HasPermissionAttribute(String permission,IServiceScopeFactory serviceScopeFactory):base()
    {
        var serviceScope = serviceScopeFactory.CreateScope();
        _context = serviceScope.ServiceProvider.GetRequiredService<AdminDbContext>();
        _userManager = serviceScope.ServiceProvider.GetRequiredService<UserManager<AdminUser>>();
        _permission=permission;
        _hostingEnvironment=serviceScope.ServiceProvider.GetRequiredService<IHostingEnvironment>();

    }
    public override async void OnActionExecuting(ActionExecutingContext context)
    {      
    // base.OnActionExecuting(context);
    user = await _userManager.GetUserAsync(context.HttpContext.User);
    string webRootPath = _hostingEnvironment.WebRootPath;

    if (!HasPermission(_permission)) {  
        Console.WriteLine("condition passed");  
        context.Result = new RedirectToActionResult("UnauthorizedAccess", "UserGroup", null);
    }
   }
}

和继承人控制器

public class UserGroupController : Controller
{

      public UserGroupController(UserManager<AdminUser> userManager,      RoleManager<Role> roleManager, ILoggerFactory loggerFactory,AdminDbContext adminDbContext,IHttpContextAccessor httpContextAccessor)
    {
        _logger = loggerFactory.CreateLogger<UserGroupController>();
        _adminDbContext = adminDbContext;
        _roleManager = roleManager;
        _userManager=userManager;
        _httpContextAccessor=httpContextAccessor;

    }

           [TypeFilter(typeof(HasPermissionAttribute),
    Arguments = new object[] { "DELETE" })]
    public async Task<IActionResult> Delete(int? id)
    {        
        if (id == null)
        {
            return NotFound();
        }

        var role = await _roleManager.FindByIdAsync(id.ToString());

        if (role == null)
        {
            return NotFound();
        }

        return View(role.ToViewModel());
    }
 }

1 个答案:

答案 0 :(得分:0)

这似乎对我有用......(我刚刚离开你的授权逻辑)

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        context.Result = new RedirectToActionResult("Index", "Home", null);
    }
}

应用于控制器......

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [MyActionFilter]
    public IActionResult About()
    {
        return View();
    }
}

现在,“关于”操作始终会重定向到“索引”操作。