-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (69 loc) · 3.4 KB
/
Program.cs
File metadata and controls
82 lines (69 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Microsoft.EntityFrameworkCore;
using Project001.data;
using Project001.Repositories;
using Project001.Interfaces;
using Project001.models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Project001.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// ✅ Register your DbContext BEFORE builder.Build()
builder.Services.AddDbContext<ApplicationDBContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// At this point we're configuring the users <AppUser> to be available on our application
// Given to us by Identity.Core
builder.Services.AddIdentity<AppUser, IdentityRole>((options) => // This second type (IdentityRole) means that you want roles in your application
{
// We're trying to ensure secure passwords on the user account
// These are the important ones as most of the others are enforced by default
// This is proof that you can actually do more than configure the password
// options.User.AllowedUserNameCharacters += " ";
options.Password.RequireDigit = options.Password.RequireLowercase = options.Password.RequireUppercase = options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 8;
}).AddEntityFrameworkStores<ApplicationDBContext>();
// .AddDefaultTokenProviders(); => Adds providers for generating security tokens used for Email Confirmation, Password Reset and 2FA
// Adding authentication and schemes to the application
builder.Services.AddAuthentication(options =>
{
// Configure the settings for the User Auth to the defaults for the JWT authentication
options.DefaultAuthenticateScheme = options.DefaultChallengeScheme = options.DefaultForbidScheme = options.DefaultScheme = options.DefaultSignInScheme = options.DefaultSignOutScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
// Pass in the JWT parameters needed to sign your users into the application
// This provides the JWT dependency with what is needed to create a JWT like
// The Signing key, the Issuer and the Audience
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = builder.Configuration["JWT:Issuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["JWT:Audience"],
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(
System.Text.Encoding.UTF8.GetBytes(builder.Configuration["JWT:SigningKey"])
)
};
});
// Links me up to my Interface/Repo Design Pattern
builder.Services.AddScoped<IStockRepo, StockRepository>();
builder.Services.AddScoped<ICommentRepo, CommentRepository>();
builder.Services.AddScoped<ITokenService, TokenService>();
builder.Services.AddScoped<IPortfolioRepo, PortfolioRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// Enables the JWT Authentication for the application
app.UseAuthentication(); // NB: This always comes before the Authorization
app.UseAuthorization();
app.MapControllers(); // Connects to the controllers
app.Run();