Skip to content

Commit 2c3b31c

Browse files
committed
add Docker support; run the two apps using Docker Compose
1 parent cdd1ef8 commit 2c3b31c

13 files changed

+173
-6
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ ClientBin/
217217
*.dbmdl
218218
*.dbproj.schemaview
219219
*.jfm
220-
*.pfx
221220
*.publishsettings
222221
orleans.codegen.cs
223222

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
11
# JWT Auth Demo
22

33
This repository demos an ASP.NET Core web API application using JWT auth, and an integration testing project for a set of actions including login, logout, refresh token, impersonation, authentication, and authorization.
4+
5+
## Solution Structure
6+
7+
This repository includes two applications: an Angular SPA in the `angular` folder, and an ASP.NET Core web API app in the `webapi` folder. The SPA makes HTTP requests to the server side (the `webapi` app) using an API BaseURL `https://localhost:5001`. The API BaseURL is set in the `environment.ts` file and the `environment.prod.ts` file, which can be modified based on your situation.
8+
9+
- `angular`
10+
The SPA is served using NGINX on Docker. The application demonstrates JWT authorization in the front-end.
11+
- `webapi`
12+
The ASP.NET Core web API app is served by Kestrel on Docker. This app has implemented HTTPS support.
13+
14+
## Usage
15+
16+
The demo is configured to run by Docker Compose. The services are listed in the `docker-compose.yml` file. You can launch the demo by the following command.
17+
18+
```bash
19+
docker-compose up --build --remove-orphans
20+
```
21+
22+
You can also move the folders around to consolidate the solution to be one ASP.NET Core web app using SPA service.
23+
24+
## Screenshots
25+
26+
- **Front-end** ([http://localhost:8080](http://localhost:8080))
27+
28+
![angular app](./localhost_8080.png)
29+
30+
- **Back-end** ([https://localhost:5001](https://localhost:5001))
31+
32+
![web api](./localhost_5001.png)

Diff for: angular/Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM node:alpine as builder
2+
3+
WORKDIR /app
4+
COPY package.json package-lock.json ./
5+
ENV CI=1
6+
RUN npm ci
7+
8+
COPY . .
9+
RUN npm run build -- --prod --output-path=/dist
10+
11+
# Deploy our Angular app to NGINX
12+
FROM nginx:alpine
13+
14+
## Replace the default nginx index page with our Angular app
15+
RUN rm -rf /usr/share/nginx/html/*
16+
COPY --from=builder /dist /usr/share/nginx/html
17+
18+
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
19+
COPY ./nginx/gzip.conf /etc/nginx/gzip.conf
20+
21+
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Diff for: angular/nginx/gzip.conf

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
gzip on;
2+
gzip_vary on;
3+
gzip_http_version 1.0;
4+
gzip_comp_level 5;
5+
gzip_types
6+
application/atom+xml
7+
application/javascript
8+
application/json
9+
application/rss+xml
10+
application/vnd.ms-fontobject
11+
application/x-font-ttf
12+
application/x-web-app-manifest+json
13+
application/xhtml+xml
14+
application/xml
15+
font/opentype
16+
image/svg+xml
17+
image/x-icon
18+
text/css
19+
text/plain
20+
text/x-component;
21+
gzip_proxied no-cache no-store private expired auth;
22+
gzip_min_length 256;
23+
gunzip on;

Diff for: angular/nginx/nginx.conf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
user nginx;
2+
3+
worker_processes auto;
4+
5+
events { worker_connections 1024; }
6+
7+
http {
8+
include /etc/nginx/mime.types;
9+
include /etc/nginx/gzip.conf;
10+
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
11+
server_tokens off;
12+
13+
sendfile on;
14+
keepalive_timeout 29; # Adjust to the lowest possible value that makes sense for your use case.
15+
client_body_timeout 10; client_header_timeout 10; send_timeout 10;
16+
17+
server {
18+
listen 80;
19+
server_name $hostname;
20+
root /usr/share/nginx/html;
21+
22+
add_header X-Frame-Options DENY;
23+
add_header X-Content-Type-Options nosniff;
24+
add_header X-Frame-Options "SAMEORIGIN";
25+
26+
location / {
27+
try_files $uri $uri/ /index.html;
28+
}
29+
}
30+
}

Diff for: angular/src/app/management/management.component.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
<p>management works!</p>
1+
<div class="container p-3">
2+
<p>TODO: impersonation related components</p>
3+
<p>management works!</p>
4+
</div>

Diff for: docker-compose.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3.8'
2+
3+
services:
4+
angular:
5+
build: ./angular
6+
ports:
7+
- '8080:80'
8+
depends_on:
9+
- api
10+
restart: always
11+
12+
api:
13+
build: ./webapi
14+
ports:
15+
- '5001'
16+
environment:
17+
- ASPNETCORE_ENVIRONMENT=Development
18+
- ASPNETCORE_URLS=https://+:5001;http://+:5000
19+
- ASPNETCORE_HTTPS_PORT=5001
20+
- ASPNETCORE_Kestrel__Certificates__Default__Password=mypassword123
21+
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
22+
volumes:
23+
- ./webapi/https/aspnetapp.pfx:/https/aspnetapp.pfx:ro
24+
restart: always

Diff for: localhost_5001.png

195 KB
Loading

Diff for: localhost_8080.png

166 KB
Loading

Diff for: webapi/Dockerfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
1+
ARG VERSION=3.1-alpine
2+
3+
FROM mcr.microsoft.com/dotnet/core/sdk:$VERSION AS build
24
WORKDIR /app
35

46
COPY ./*.sln .
@@ -15,8 +17,7 @@ WORKDIR /app/JwtAuthDemo
1517
RUN dotnet publish -c Release -o /out --no-restore
1618

1719

18-
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS runtime
20+
FROM mcr.microsoft.com/dotnet/core/aspnet:$VERSION AS runtime
1921
WORKDIR /app
2022
COPY --from=build /out ./
21-
ENV ASPNETCORE_URLS http://*:5000
2223
ENTRYPOINT ["dotnet", "JwtAuthDemo.dll"]

Diff for: webapi/JwtAuthDemo/Program.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System;
2+
using System.Security.Authentication;
13
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Server.Kestrel.Core;
25
using Microsoft.Extensions.Hosting;
36

47
namespace JwtAuthDemo
@@ -14,7 +17,18 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
1417
Host.CreateDefaultBuilder(args)
1518
.ConfigureWebHostDefaults(webBuilder =>
1619
{
17-
webBuilder.UseStartup<Startup>();
20+
webBuilder.ConfigureKestrel(serverOptions =>
21+
{
22+
serverOptions.Limits.MinRequestBodyDataRate = new MinDataRate(100, TimeSpan.FromSeconds(10));
23+
serverOptions.Limits.MinResponseDataRate = new MinDataRate(100, TimeSpan.FromSeconds(10));
24+
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
25+
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1);
26+
serverOptions.ConfigureHttpsDefaults(listenOptions =>
27+
{
28+
listenOptions.SslProtocols = SslProtocols.Tls12;
29+
});
30+
})
31+
.UseStartup<Startup>();
1832
});
1933
}
2034
}

Diff for: webapi/https/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Create SSL Certificate
2+
3+
[document](https://docs.microsoft.com/en-us/aspnet/core/security/docker-compose-https)
4+
5+
## Windows using Linux containers
6+
7+
Generate certificate and configure local machine:
8+
9+
```bash
10+
# in the current folder
11+
dotnet dev-certs https -ep aspnetapp.pfx -p mypassword123
12+
dotnet dev-certs https --trust
13+
14+
15+
```
16+
17+
```powershell
18+
dotnet dev-certs https -ep $env:USERPROFILE\.aspnet\https\aspnetapp.pfx -p mypassword123
19+
```
20+
21+
```cmd
22+
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p mypassword123
23+
```

Diff for: webapi/https/aspnetapp.pfx

2.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)