Store does not implement IUserRoleStore ASP.NET Core Identity

In Startup.cs, I was missing AddRoles so services.AddDefaultIdentity<PortalUser>() .AddEntityFrameworkStores<ApplicationDbContext>(); should be services.AddDefaultIdentity<PortalUser>() .AddRoles<IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>(); Note: The order is critical. AddRoles must come before AddEntityFrameworkStores

JavaScriptSerializer is not allowed in .net core project?

In .net core the most common way of serializing and deserializing objects (JSON) using Newtonsoft.Json. You need to install the Nuget Package of Newtonsoft.Json add using a statement like: using Newtonsoft.Json; and use it as: object o = JsonConvert.DeserializeObject(json1); string json2 = JsonConvert.SerializeObject(o, Formatting.Indented);

Remove “Server” header from ASP.NET Core 2.1 application

This solution works on IIS 10+ version and allows to remove x-powered-by and server headers in server response. In IIS 10 a new attribute was added: removeServerHeader. We need to create web.config file in asp.net core application with following content: <?xml version=”1.0″ encoding=”utf-8″?> <configuration> <system.webServer> <security> <requestFiltering removeServerHeader=”true” /> </security> <httpProtocol> <customHeaders> <remove name=”X-Powered-By” /> … Read more

How do I get a reference to an IHostedService via Dependency Injection in ASP.NET Core?

Current workaround from mentioned git page: services.AddSingleton<YourServiceType>(); services.AddSingleton<IHostedService>(p => p.GetRequiredService<YourServiceType>()); Or, if your service implements some other interfaces: services.AddSingleton<YourServiceType>(); services.AddSingleton<IYourServiceType>(p => p.GetRequiredService<YourServiceType>()); services.AddSingleton<IHostedService>(p => p.GetRequiredService<YourServiceType>()); This creates your service as hosted (runs and stops at host’s start and shutdown), as well as gets injected as depedency wherever you require it to be. Update: I don’t … Read more

Different Minimum Level Logs Serilog

The setting you’re looking for is restrictedToMinimumLevel. This GitHub issue shows some examples of this, but for your example, you just need to add restrictedToMinimumLevel to your Args for RollingFile: “Serilog”: { “Using”: [ “Serilog.Sinks.Console” ], “MinimumLevel”: “Debug”, “WriteTo”: [ { “Name”: “RollingFile”, “IsJson”: true, “Args”: { “pathFormat”: “C:\\Logs\\Log-{Hour}.json”, “formatter”: “Serilog.Formatting.Json.JsonFormatter, Serilog”, “restrictedToMinimumLevel”: “Warning” } … Read more

Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

Update: This has been made easier with 2.2 Tooling. Make sure that your dotnet –version SDK version is at least 2.2.100, even when buidling 2.1 applications Just add a versionless package reference to your project while keeping the Microsoft.NET.Sdk: <Project Sdk=”Microsoft.NET.Sdk”> <PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=”Microsoft.AspNetCore.Mvc.Testing” Version=”2.1.1″ /> <PackageReference Include=”Microsoft.AspNetCore.App” /> <!– other references … Read more

How to specify the view location in asp.net core mvc when using custom locations?

Great news… In ASP.NET Core 2 and up, you don’t need a custom ViewEngine or even ExpandViewLocations anymore. Using the OdeToCode.AddFeatureFolders Package This is the easiest way… K. Scott Allen has a nuget package for you at OdeToCode.AddFeatureFolders that is clean and includes optional support for areas. Github: https://github.com/OdeToCode/AddFeatureFolders Install the package, and it’s as … Read more