• Generating links inside a .NET Core Tag Helper

    Previously when generating links inside any non view code I'd always try and get hold of an instance of IUrlHelper, but I've found a simpler way that has been available since .NET Core 2.2.

    LinkGenerator can be injected into a tag helper or any class. It has all the useful methods of IUrlHelper, with fewer dependencies. It only asks for HttpContext if it absolutely needs it, which in many cases it does not.

    Inject it into your class like so:

    private readonly LinkGenerator _linkGenerator;

    public NogginTagHelper(LinkGenerator linkGenerator)

    {

    _linkGenerator = linkGenerator;

    }

    Using the …

  • Using URL helper inside your .NET Core Tag Helper

    I've discovered a better way of doing this. Check out my new post on using LinkGenerator instead.

    If you're writing a tag helper and would like to generate links using IUrlHelper then you can not inject this directly. You need to inject an IUrlHelperFactory and then there are a few hoops that you need to jump through.

    This is how to set up the UrlHelper inside you tag helper constructor:

    private readonly IUrlHelper _urlHelper;

    public NogginTagHelper(IUrlHelperFactory urlHelperFactory, IActionContextAccessor contextAccessor)

    {

    _urlHelper = urlHelperFactory.GetUrlHelper(contextAccessor. …

  • Installing a free SSL Cert on your IIS .NET Core MVC website

    SSL certs are getting more and more important on the web as we want to make sure that our websites are safe and trustworthy. I’ve put off installing them on my own personal web site projects because of the cost and the work involved in keeping them up to date.

    Let’s Encrypt is an open Certificate Authority that is trusted and issues free 3 month certificates. They have an API that lets you automate getting these certificates and there are several tools for Linux and Windows that use this API to save you the work of installing and keeping your certs up to date.

    I used the Windows tool Certify …

  • Forms Authentication in .NET Core (AKA Cookie Authentication)

    In .NET Core MVC you're encourages to use .NET Identity, but you don't have to. You can manage your own user identities and you use forms authentication which is now called Cookie Authentication (which is a better name really).

    You need to install the Microsoft.AspNetCore.Authentication.Cookies nuget package.

    There is some configuration that needs to go in startup.cs:

    public void ConfigureServices(IServiceCollection services)

    {

    services

    .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)

    .AddCookie(options => {

    options.AccessDeniedPath = "/ …