|
1 | | -using System.IdentityModel.Tokens.Jwt; |
2 | | -using System.Security.Claims; |
3 | | -using Microsoft.AspNetCore.Authentication.JwtBearer; |
4 | 1 | using Microsoft.EntityFrameworkCore; |
5 | | -using Microsoft.IdentityModel.Tokens; |
6 | 2 | using WebMinRouteGroup; |
7 | 3 | using WebMinRouteGroup.Data; |
8 | 4 | using WebMinRouteGroup.Services; |
9 | 5 |
|
10 | 6 | var builder = WebApplication.CreateBuilder(args); |
11 | 7 |
|
12 | | -builder.Services.AddAuthentication(o => |
13 | | -{ |
14 | | - o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; |
15 | | -}).AddJwtBearer(o => |
16 | | -{ |
17 | | - o.TokenValidationParameters = new TokenValidationParameters |
18 | | - { |
19 | | - ValidIssuer = "http://localhost:5000", |
20 | | - ValidAudience = "http://localhost:5000", |
21 | | - IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String("Efgwc3/0vEGk/+eS0HP0Hw==")), |
22 | | - ValidateIssuer = true, |
23 | | - ValidateAudience = true, |
24 | | - ValidateLifetime = false, |
25 | | - ValidateIssuerSigningKey = true |
26 | | - }; |
27 | | -}); |
28 | | - |
29 | | -builder.Services.AddAuthorization(o => o.AddPolicy("AdminsOnly", |
30 | | - b => b.RequireClaim("admin", "true"))); |
31 | | - |
32 | 8 | builder.Services.AddTransient<ITodoService, TodoService>(); |
33 | 9 | builder.Services.AddSingleton<IEmailService, EmailService>(); |
34 | 10 |
|
35 | | -builder.Services.AddEndpointsApiExplorer(); |
36 | | -builder.Services.AddSwaggerGen(); |
| 11 | + |
37 | 12 | builder.Services.AddDbContext<TodoGroupDbContext>(options => |
38 | 13 | { |
39 | 14 | var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
|
46 | 21 | var db = scope.ServiceProvider.GetService<TodoGroupDbContext>(); |
47 | 22 | db?.Database.MigrateAsync(); |
48 | 23 |
|
49 | | -app.UseAuthentication(); |
50 | | -app.UseAuthorization(); |
51 | | - |
52 | | -if (app.Environment.IsDevelopment()) |
53 | | -{ |
54 | | - app.UseSwagger(); |
55 | | - app.UseSwaggerUI(); |
56 | | -} |
57 | | - |
58 | | -app.MapGet("/", () => "Hello World!"); |
59 | | - |
60 | | -app.MapGet("/token", (HttpContext context) => |
61 | | -{ |
62 | | - var username = context.Request.Headers["username"].ToString(); |
63 | | - var password = context.Request.Headers["password"].ToString(); |
64 | | - |
65 | | - if (username.Equals("admin") && password.Equals("admin")) |
66 | | - { |
67 | | - var issuer = "http://localhost:5000"; |
68 | | - var audience = "http://localhost:5000"; |
69 | | - var securityKey = new SymmetricSecurityKey(Convert.FromBase64String("Efgwc3/0vEGk/+eS0HP0Hw==")); |
70 | | - var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); |
71 | | - |
72 | | - var tokenDescriptor = new SecurityTokenDescriptor |
73 | | - { |
74 | | - Audience = audience, |
75 | | - Issuer = issuer, |
76 | | - SigningCredentials = credentials, |
77 | | - Expires = DateTime.UtcNow.AddMinutes(2), |
78 | | - Subject = new ClaimsIdentity(new [] |
79 | | - { |
80 | | - new Claim("admin", "true") |
81 | | - }) |
82 | | - }; |
83 | | - |
84 | | - var jwtTokenHandler = new JwtSecurityTokenHandler(); |
85 | | - var token = jwtTokenHandler.CreateToken(tokenDescriptor); |
86 | | - |
87 | | - return Results.Ok(new |
88 | | - { |
89 | | - access_token = jwtTokenHandler.WriteToken(token) |
90 | | - }); |
91 | | - } |
92 | | - |
93 | | - return Results.Unauthorized(); |
94 | | -}).AllowAnonymous(); |
95 | | - |
96 | | -app.MapGet("/admin", () => "Authorized Endpoint") |
97 | | - .RequireAuthorization("AdminsOnly"); |
98 | | - |
99 | 24 | // todoV1 endpoints |
100 | 25 | app.MapGroup("/todos/v1") |
101 | 26 | .MapTodosApiV1() |
|
0 commit comments