-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
216 lines (179 loc) · 5.64 KB
/
Program.cs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using WatchNest.Models;
using WatchNest.Models.Implementation;
using WatchNest.Models.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using WatchNest.Constants;
using WatchNest.Swagger;
using Microsoft.AspNetCore.Authentication.Cookies;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(opts =>
{
opts.CacheProfiles.Add("NoCache", new CacheProfile()
{
Location = ResponseCacheLocation.None,
NoStore = true
});
opts.CacheProfiles.Add("Any-60", new CacheProfile()
{
Location = ResponseCacheLocation.Any,
Duration = 60
});
}); //For API
var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>();
builder.Services.AddCors(opts =>
{
opts.AddDefaultPolicy(cfg =>
{
cfg.WithOrigins(allowedOrigins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
opts.AddPolicy(name: "AnyOrigin",
cfg =>
{
cfg.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddDbContext<ApplicationDbContext>(opts =>
{
//Change to your own connection, set to localDB
opts.UseSqlServer(builder.Configuration["ConnectionStrings:LocalDB"]);
});
builder.Services.AddDistributedSqlServerCache(opts =>
{
//Change to your own connection, set to localDB
opts.ConnectionString = builder.Configuration.GetConnectionString("LocalDB");
opts.SchemaName = "dbo";
opts.TableName = "AppCache";
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddIdentity<ApiUsers,IdentityRole>(opts =>
{
//Password Requirements
opts.Password.RequireDigit = true;
opts.Password.RequireLowercase = true;
opts.Password.RequireUppercase = true;
opts.Password.RequireNonAlphanumeric = true;
opts.Password.RequiredLength = 10;
}).AddEntityFrameworkStores<ApplicationDbContext>(); //Uses DbContext for storage
//Prevent forgery tokens
builder.Services.AddAuthentication(opts =>
{
opts.DefaultScheme =
opts.DefaultAuthenticateScheme =
opts.DefaultSignInScheme =
opts.DefaultSignOutScheme =
opts.DefaultChallengeScheme =
opts.DefaultForbidScheme =
JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(opts =>
{
opts.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"])),
ClockSkew = TimeSpan.Zero // No extra buffer time
};
opts.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
context.Token = context.Request.Cookies["AuthToken"];
return Task.CompletedTask;
}
};
}).AddCookie(opts =>
{
opts.Cookie.Name = "AuthToken";
opts.Cookie.HttpOnly = true; // Ensure the cookie is not accessible via JavaScript
opts.Cookie.SecurePolicy = CookieSecurePolicy.Always; // Only send over HTTPS
opts.ExpireTimeSpan = TimeSpan.FromMinutes(30); // Expire cookies after 30 mins
});
builder.Services.AddSwaggerGen(opts =>
{
// Authorization and endpoints testing for client side
opts.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description ="Please enter token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme ="bearer"
});
//Adjusted Endpoint that Require Authorization and/or login
opts.OperationFilter<AuthRequirementFilter>();
opts.EnableAnnotations();
});
builder.Services.AddAuthorization();
builder.Services.AddScoped<IUserServices, UserService>();
builder.Services.AddScoped<ISeriesService, SeriesService>();
builder.Services.AddScoped<IAdminService, AdminService>();
builder.Services.AddHttpsRedirection(opts =>
{
opts.HttpsPort = 44350;
});
builder.Services.AddResponseCaching();
var app = builder.Build();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCaching();
app.UseHttpsRedirection();
//Global No cache
app.Use((context, next) =>
{
context.Response.Headers["cache-control"] = "no-cache, no-store";
return next.Invoke();
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
app.MapGet("/auth/test/1",
[Authorize,ResponseCache(NoStore = true)]
() =>
{
return Results.Ok("You are authorized!");
});
app.MapGet("/auth/test/rbac",
[ResponseCache(CacheProfileName = "NoCache")]
[Authorize]
(HttpContext httpContext) =>
{
var user = httpContext.User;
if (user.IsInRole(RoleNames.Administrator))
{
return Results.Ok("You are Authorized as Admin!");
}
else if (user.IsInRole(RoleNames.User))
{
return Results.Ok("You are not an Admin! Go back!");
}
return Results.Forbid();
});
}
else
{
app.UseExceptionHandler("/error");
}
app.MapControllers();
app.Map("/",()=> "Add \"/swagger/index.html\" to the end of the URL to access Swagger Testing ");
app.MapGet("/error", () => Results.Problem());
app.Run();