-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (125 loc) · 4.93 KB
/
Program.cs
File metadata and controls
156 lines (125 loc) · 4.93 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
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
using System.Text;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using SimpleNotesApp.Data;
using SimpleNotesApp.Models;
using SimpleNotesApp.Services;
var builder = WebApplication.CreateBuilder(args);
// Add localisation services
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
// database connections -> sqlite
builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("database")));
// add the identity service and configure user authentication settings
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options => {
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 6;
// Email confirmation settings
options.SignIn.RequireConfirmedEmail = true;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
// Add email service
builder.Services.AddTransient<IEmailService, EmailService>();
// cookie-based authentication settings
builder.Services.ConfigureApplicationCookie(options => {
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
options.ExpireTimeSpan = TimeSpan.FromDays(1);
options.SlidingExpiration = true;
});
// Configure the authentication process
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "smart";
options.DefaultChallengeScheme = "smart";
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
ValidAudience = builder.Configuration["JWT:ValidAudience"],
ValidIssuer = builder.Configuration["JWT:ValidIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:Secret"]))
};
})
.AddPolicyScheme("smart", "Smart Scheme", options =>
{
// api calls or requests containing an Authorization header will use JWT authentication
options.ForwardDefaultSelector = context =>
{
if (context.Request.Path.StartsWithSegments("/api") ||
context.Request.Headers.ContainsKey("Authorization"))
{
return JwtBearerDefaults.AuthenticationScheme; // use JWT in API requests
}
return CookieAuthenticationDefaults.AuthenticationScheme; // use cookie authentication on others
};
});
// Add services to the container.
builder.Services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "tr-tr", "en-us" };
options.SetDefaultCulture("tr-tr")
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
options.RequestCultureProviders.Insert(0, new CookieRequestCultureProvider()
{
CookieName = "CULTURE"
});
});
builder.Services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseStatusCodePagesWithReExecute("/Error/{0}");
// Localization middleware
var locOptions = app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{lang=tr-tr}/{controller=Home}/{action=Index}/{id?}",
constraints: new { lang = "tr-tr|en-us" });
app.MapControllerRoute(
name: "catch-all-with-lang",
pattern: "{lang}/{*path}",
constraints: new { lang = "tr-tr|en-us" },
defaults: new { controller = "Error", action = "HandleError", statusCode = 404 });
app.MapControllerRoute(
name: "catch-all",
pattern: "{*path}",
defaults: new { controller = "Error", action = "HandleError", statusCode = 404 });
app.Run();