How to enable dotnet watch in Jetbrains Raider

Hi guys, while working on my pet API project in C# and .NET Core I realized that I miss having the debugger ‘watch’ for file changes. The standard way of doing this in .NET Core 2.0 is to setup dotnet-watch . It seems that starting with .NET Core 2.1 this step isn’t going to be necessary, so one less step to do. Until then you can follow the official tutorial for setting up dotnet-watch....

08 February, 2019 · 1 min · 205 words · Alexandru Bucur

Setting up JWT and Identity Authorization/Authentication in ASP .NET Core

Continuing my foray into ASP .NET Core, and making sure I get outside my comfort zone, I got into the situation that I want to be able to easily access the logged in user information in my API request. There are several versions available online, some unfortunately out of date (mostly because ASP .NET Core 2.0 is relatively new and things changed significantly in Identity between 1.0 and 2.0). First version I stumbled upon recommended extending the IHttpContextAccessor....

08 February, 2019 · 3 min · 484 words · Alexandru Bucur

ASP .NET Core shared Current User

As a quick update to the previous article explaining how to set up JWT and Identity I’ve updated the currentUser method to use dependency injection. 1 2 3 4 5 6 7 8 9 10 namespace Craidd.Extensions { public static class IHttpContextAccessorExtension { public static async Task<User> CurrentUser(this IHttpContextAccessor httpContextAccessor) { IUsersService users = httpContextAccessor.HttpContext.RequestServices.GetService(typeof(IUsersService)) as IUsersService; return await users.UserManager.GetUserAsync(httpContextAccessor.HttpContext.User); } } } In my case, IUsersService is a wrapper that contains dbContext, UserManager and SignInManager....

08 February, 2019 · 1 min · 199 words · Alexandru Bucur

JSON.NET append to existing key

A quick example for appending to an existing key in a JSON.NET object, since for me at least it wasn’t clear in the documentation. 1 2 3 4 5 JObject myJson = new JObject(new JProperty("errors", new JObject())); /// we can now reference it as follows just match the type myJson["errors"].Value<JObject>().Add(new JProperty("title", title)); Small update: You can even add multiple entries to an JArray the same way 1 2 /// assuming errors is a JArray, this will automatically append new entries ErrorReponse["errors"]....

08 February, 2019 · 1 min · 84 words · Alexandru Bucur

Simple JSON API ASP .NET Core Error handling class

As a follow up to my investigation on API Responses I’ve decided to not create yet another random format and just stick to JSON API even if it means more parsing on the front-end. That being said, ASP .NET Core’s standard response format for ModelState validation doesn’t follow the convention, so I’ve written a small helper class. Unfortunately in .NET Core 2.0 I couldn’t figure out if there’s a way of overwriting the default response format (maybe with an extension method?...

08 February, 2019 · 3 min · 476 words · Alexandru Bucur

How to get the user id after registering an user in ASP .NET Core Identity

Getting back to work on my side project I got into finishing the user registration trough the API. Following the JSON API spec, it requires returning a 201 created and the resource id when creating a database entry. For somebody used with ASP this might seem trivial but it’s not that obvious when creating a new user. Let’s take a simple example: 1 2 3 4 5 6 var user = new ApplicationUser { UserName = model....

08 February, 2019 · 1 min · 149 words · Alexandru Bucur

Dynamic Policy Claims in ASP.NET Core using JWT Tokens (and Role Claims)

Hi guys, as a developer I bet everybody is a little bit ’lazy’ and likes optimizing their workflow. My latest aha moment of laziness was when I was going trough my pet project and working on implementing Policy-based authorization and deciding that adding AddPolicy every single time I want to implement a new Role Claim in the database is counter intuitive. I want to say a big thank you to Jerrie Pelser for the initial work on this....

27 January, 2019 · 4 min · 711 words · Alexandru Bucur

How to rename Asp .Net Core 2.2 Identity Tables to not have AspNet prefix in EF Core

Not sure if the code below should be considered a hack, but the easiest way of removing the AspNet prefix is to iterate through the models. 1 2 3 4 5 6 7 8 9 10 11 12 protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { var table = entityType.Relational().TableName; if (table.StartsWith("AspNet")) { entityType.Relational().TableName = table.Substring(6); } }; } Found the initial code in a 2014 asp net issue and since copy paste didn’t cut it, did a quick update of the method / property names....

16 December, 2018 · 1 min · 109 words · Alexandru Bucur

The confusing way of sending emails in .NET Core and other goodies

Since I finally had some time to play more with C# I’ve encountered my next challenge when wanting to implement user registration with a confirmation email. Even though the official documentation is clear on how to handle the one time code generation I didn’t want to use Sendgrid but plain old SMTP authentication. Further more I don’t agree with having the mail body as a string in my registration task....

08 December, 2018 · 2 min · 290 words · Alexandru Bucur