public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var(status, user) = await cookieManager.GetUserSafe(context.HttpContext.Request); if (user == null) { logger.LogWarning($"Неавторизованный вызов {context.HttpContext.Request.Path} {status:G}"); cookieManager.Clear(context.HttpContext.Response); context.Result = new UnauthorizedResult(); return; } if (roles.Any() && !roles.Contains(user.Role)) { logger.LogWarning($"Недостаточно прав для {context.HttpContext.Request.Path} {user.Id}"); context.Result = new ObjectResult("Неверная роль") { StatusCode = 401, }; } else { context.ActionArguments["user"] = user; await next(); } }
public void LogOut() => userCookieManager.Clear(Response);