Can I use a Tag Helper in a custom Tag Helper that returns html?

No you cannot. TagHelpers are a Razor parse time feature. One alternative is creating a TagHelper and manually invoking its ProcessAsync/Process method. Aka: var anchorTagHelper = new AnchorTagHelper { Action = “Home”, }; var anchorOutput = new TagHelperOutput(“a”, new TagHelperAttributeList(), (useCachedResult, encoder) => new HtmlString()); var anchorContext = new TagHelperContext( new TagHelperAttributeList(new[] { new TagHelperAttribute(“asp-action”, … Read more

Javascript in a View Component

What I decided to do is write a ScriptTagHelper that provides an attribute of “OnContentLoaded”. If true, then I wrap the inner contents of the script tag with a Javascript function to execute once the document is ready. This avoids the problem with the jQuery library having not loaded yet when the ViewComponent’s script fires. … Read more

Get role/s of current logged in user in ASP.NET Core MVC

You may want to consider trying to load the actual ApplicationUser object via the FindByEmail() or some other method and passing that object into the GetRolesAsync() method as seen below : // Resolve the user via their email var user = await _userManager.FindByEmailAsync(model.Email); // Get the roles for the user var roles = await _userManager.GetRolesAsync(user); … Read more

Logging within Program.cs

Accidentally stumbled upon the answer after googling a bit more. using System; using Microsoft.Extensions.Logging; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { var logFactory = new LoggerFactory() .AddConsole(LogLevel.Debug) .AddDebug(); var logger = logFactory.CreateLogger<Type>(); logger.LogInformation(“this is debug log”); } } } Kudos to https://askguanyu.wordpress.com/2016/09/26/net-core-101-e06-net-core-logging/

Async IServiceProvider in .NET Core DI

Although it is theoretically possible to use async/await during object resolution, you should consider the following constraints: Constructors can’t be asynchronous, and Construction of object graphs should be simple, reliable and fast Because of these constraints, it’s best to postpone everything that involves I/O until after the object graph has been constructed. So instead of … Read more

Return HTTP 403 using Authorize attribute in ASP.Net Core

After opening an issue here, it appears this actually should work…sort of. In your Startup.Configure, if you just call app.UseMvc() and don’t register any other middleware, you will get 401 for any auth-related errors (not authenticated, authenticated but no permission). If, however, you register one of the authentication middlewares that support it, you will correctly … Read more

AutoValidateAntiForgeryToken vs. ValidateAntiForgeryToken

From AutoValidateAntiforgeryTokenAttribute documentation: An attribute that causes validation of antiforgery tokens for all unsafe HTTP methods. An antiforgery token is required for HTTP methods other than GET, HEAD, OPTIONS, and TRACE. It can be applied at as a global filter to trigger validation of antiforgery tokens by default for an application. AutoValidateAntiforgeryTokenAttribute allows to apply … Read more

Read request body twice

If you’re using application/x-www-form-urlencoded or multipart/form-data, you can safely call context.Request.ReadFormAsync() multiple times as it returns a cached instance on subsequent calls. If you’re using a different content type, you’ll have to manually buffer the request and replace the request body by a rewindable stream like MemoryStream. Here’s how you could do using an inline … Read more