public async Task OnActionExecutionAsync( ActionExecutingContext context, ActionExecutionDelegate next) { // do something before the action executes // if the controller is an aservicecontroller, then // fetch the details, otherwise, do nothing. if (context.Controller is AServiceController controller) { HttpRequestMessage request = controller.CreateRequestToService( HttpMethod.Get, _configuration["ServiceEndpoints:AccountDetails"]); HttpResponseMessage response = await controller.HttpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { // setting "Result" in a filter short-circuits the rest // of the MVC pipeline // but i won't do that, i should just log it. } else { var jsonString = await response.Content.ReadAsStringAsync(); ApiAccountDetails details = JsonConvert.DeserializeObject <ApiAccountDetails>(jsonString); controller.ViewData["accountDetails"] = details; controller.Account = details; } } await next(); }
public ApiAccountDetails Details() { // if we want to know which user is logged in or which roles he has // apart from [Authorize] attribute... // we have User.Identity.IsAuthenticated // User.IsInRole("admin") // User.Identity.Name if (!User.Identity.IsAuthenticated) { _logger.LogInformation(""); return(null); } var details = new ApiAccountDetails { Username = User.Identity.Name, Roles = User.Claims.Where(c => c.Type == ClaimTypes.Role) .Select(c => c.Value) }; return(details); }