Skip to content

added to JWT tutorial #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/tutorials/databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

## Working with Databases

We just learned how to build a basic [CRUD](CRUD.md) application with an in-memory database. Now, we are going to step it up notch and work with a persistent database. Meaning your data will be saved even after you shut down your application.
In [Tutorial Two: My First CRUD App](crud.md), we just learned how to build a basic CRUD application with an in-memory database. Now, we are going to step it up notch and work with a persistent database. This means that your data will be saved even after you shut down your application.

For this tutorial we will be using [SQLite database](https://www.sqlite.org/index.html) but, you may use one that works better for you.
For this tutorial we will be using [SQLite database](https://www.sqlite.org/index.html) but you may use any option that you prefer.

## Setup SQLite database
**Setup SQLite Database using Entity Framework Core(EF Core)***

**Install the following tools and packages**

Using .NET CLI / Visual Studio package manager UI, install the following packages:
Using the .NET CLI or Visual Studio package manager UI, install the following packages:

**[SQLite EF Core Database Provider](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/6.0.0-preview.5.21301.9)** : can access many different databases through plug-in libraries called [database providers](https://docs.microsoft.com/ef/core/providers/?tabs=dotnet-core-cli). The package below is the SQLite database provider for EF Core.
**[SQLite EF Core Database Provider](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/6.0.0-preview.5.21301.9)** : provides access to multiple different databases using plug-in libraries that are called [database providers](https://docs.microsoft.com/ef/core/providers/?tabs=dotnet-core-cli).

The package below is the SQLite database provider for EF Core.

```console
TodoApi>dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 6.0.0-preview.6.21352.1
```

**[Entity Framework Core tools ](https://docs.microsoft.com/ef/core/cli/dotnet)**: tools for Entity Framework Core perform design-time development tasks. For example, they create migrations, apply migrations, and generate code for a model based on an existing database.
**[Entity Framework Core tools ](https://docs.microsoft.com/ef/core/cli/dotnet)**: tools that perform design-time development tasks for Entity Framework Core. For example, they create and apply migrations and can generate code for a model based on an existing database.

```console
TodoApi>dotnet tool install --global dotnet-ef
Expand All @@ -33,9 +34,11 @@ TodoApi>dotnet add package Microsoft.EntityFrameworkCore.Design --version 6.0.0-

## Enable database creation

In order to enable database creation they are couple of steps we need to complete:
In order to create your database, there are a couple of steps we need to complete:
1. Set the database connection string.
2. Migrate your data model *(see below)* to a SQLite database.
3. Create your database and schema

**Create a data model**
```cs
class TodoItem
Expand All @@ -46,18 +49,17 @@ class TodoItem

}
```
3. Create your database and schema


## Set connection string
### Set connection string

In `Program.cs` below your app builder `var builder = WebApplication.CreateBuilder(args);` add a connection string.

```cs
var connectionString = builder.Configuration.GetConnectionString("todos") ?? "Data Source=todos.db";
```

## Add your context to your services
### Add your context to your services

In the CRUD portion of this tutorial, we used an in-memory database. Now we are going to replace the in-memory database with a persistent database.

Expand All @@ -66,7 +68,7 @@ Replace your current in-memory database implementation `builder.Services.AddDbCo
```cs
builder.Services.AddSqlite<TodoDb>(connectionString);
```
## Migrate data model
### Migrate data model

With EF Core migration tool, you can now start your first migration `InitialCreate`. In a terminal window, run the `migrations` command below:

Expand All @@ -78,7 +80,7 @@ EF Core will create a folder called Migrations in your project directory contain

![image](https://user-images.githubusercontent.com/2546640/128618497-8de76c16-cd38-48a8-9704-599c15d115f0.png)

## Create your database and schema
### Create your database and schema
Now that you have completed the migration, you can use it to create your database and schema. In a terminal window, run the `database update` command below to apply migrations to a database:

```console
Expand Down
57 changes: 56 additions & 1 deletion src/tutorials/secure-your-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,63 @@ app.MapGet("secured-route", () => "Hello, you are authorized to see this!")

A JSON Web Token (JWT) is a way of transferring information as a JSON object. Before you add authentication and authorization to your application, you will want to have a way to create the token that the user will pass to the server to authenticate its identity, and a way for your server to authorize the user based on the contents of that token.

A detailed tutorial on setting up your own discrete server for issuing and verifying JWT tokens is in progress. In the meantime, look at
In order to create and verify JWTs, use the `dev-jwts` command-line interface (CLI) tool which can be found [here](https://github.com/DamianEdwards/AspNetCoreDevJwts).

Run the `dev-jwts` exe to print the CLI help:

```
AspNetCoreDevJwts\SampleWebApi> .\DevJwts.Cli\bin\Debug\net7.0\dev-jwts.exe
USAGE:
dev-jwts [OPTIONS] <COMMAND>

EXAMPLES:
dev-jwts create
dev-jwts create -n testuser --claim scope=myapi:read
dev-jwts list
dev-jwts delete caa676ee
dev-jwts clear

OPTIONS:
-h, --help Prints help information
-v, --version Prints version information

COMMANDS:
list Lists all JWTs for the specified project
create Creates a JWT for the specified project
print Prints the details of the specified JWT
delete Deletes the JWT with the specified ID in the specified project
clear Deletes all JWTs for the specified project
key Prints the key used for signing JWTs for the specified project

```

Run the dev-jwts exe to print the CLI help:

```
AspNetCoreDevJwts\SampleWebApi> .\DevJwts.Cli\bin\Debug\net7.0\dev-jwts.exe
USAGE:
dev-jwts [OPTIONS] <COMMAND>

EXAMPLES:
dev-jwts create
dev-jwts create -n testuser --claim scope=myapi:read
dev-jwts list
dev-jwts delete caa676ee
dev-jwts clear

OPTIONS:
-h, --help Prints help information
-v, --version Prints version information

COMMANDS:
list Lists all JWTs for the specified project
create Creates a JWT for the specified project
print Prints the details of the specified JWT
delete Deletes the JWT with the specified ID in the specified project
clear Deletes all JWTs for the specified project
key Prints the key used for signing JWTs for the specified project

```

### Securing our app with JWT Bearer Authentication

Expand Down