.NET Core 3.0 (Preview 4) Web API Authentication from Scratch (Part 1): Up and Running.

With .NET Core CLI, Entity Framework Core and SQL Server.

Prerequisites

.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview4-011223
Commit: 118dd862c8
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview4-011223\
Host (useful for support):
Version: 3.0.0-preview4-27615-11
Commit: ee54d4cbd2
.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.503 [C:\Program Files\dotnet\sdk]
2.1.505 [C:\Program Files\dotnet\sdk]
3.0.100-preview4-011223 [C:\Program Files\dotnet\sdk]
dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 3.0.0-preview4.19216.3dotnet add package Microsoft.EntityFrameworkCore.Design -v 3.0.0-preview4.19216.3dotnet tool install --global dotnet-ef --version 3.0.0-preview4.19216.3
dotnet watch run
PS D:\My Documents\Tutorials\JWTAuthNETCore3\JWTAuth.API> dotnet watch run
watch : Started
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: D:\My Documents\Tutorials\JWTAuthNETCore3\JWTAuth.API
Tabbed postman for chrome can be downloaded here.
"ConnectionStrings":{
"DefaultConnection":"Server=.;Database=<your database name>;Trusted_Connection=True;ConnectRetryCount=0"
}
appsettings.json
using JWTAuth.API.Data;
using Microsoft.EntityFrameworkCore;
services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
If you open up _EFMigrationsHistory table you will see the same migration Id that we saw on Migrations folder.
PS D:\My Documents\Tutorials\JWTAuthNETCore3\JWTAuth.API> dotnet ef migrations add InitialCreate
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.0.0-preview4.19216.3 initialized 'DataContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
Done. To undo this action, use 'ef migrations remove'
PS D:\My Documents\Tutorials\JWTAuthNETCore3\JWTAuth.API> dotnet ef database update
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.0.0-preview4.19216.3 initialized 'DataContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
info: Microsoft.EntityFrameworkCore.Migrations[20402]
Applying migration '20190503225321_InitialCreate'.
Applying migration '20190503225321_InitialCreate'.
Done.
USE [JWTAuthDB]
GO
DECLARE @value INT;
SET @value = 1;
WHILE @value <= 3
BEGIN
INSERT INTO [dbo].[Values] ([Name]) VALUES (CONCAT('Value ', @value));
SET @value = @value + 1;
END;
private readonly DataContext _context;public ValuesController(DataContext context)
{
_context = context;
}
How new lines should be added to the ValuesController.
Old get methods that we are going to change.
        // GET api/values
[HttpGet]
public async Task<IActionResult> GetValues()
{
var values = await _context.Values.ToListAsync();
return Ok(values);
}
// GET api/values/5
[HttpGet("{id}")]
public async Task<IActionResult> GetValue(int id)
{
var value = await _context.Values.FirstOrDefaultAsync(x => x.Id == id);
return Ok(value);
}
using Microsoft.EntityFrameworkCore;
Notice the status is ‘200’. which is the response status code for ‘Ok’. It means the request is succeeded and the resource has been fetched and is transmitted in the message body.

Systems Design • Social Innovation • Cloud • ML