-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
217 lines (184 loc) · 6.96 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
215
216
217
using System.Reflection;
using System.Security.Claims;
using System.Text;
using Aspnet.React.Data;
using Aspnet.React.Mapping;
using Aspnet.React.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Serilog;
using Swashbuckle.AspNetCore.SwaggerGen;
var builder = WebApplication.CreateBuilder(args);
AuthOptions.KEY = builder.Configuration["JWT:Key"];
// Add services to the container.
builder.Host.UseSerilog((ctx, lc) =>
{
lc.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning);
lc.WriteTo.Console();
});
builder.Services.AddAutoMapper(typeof(AppMappingProfile));
builder.Services.AddDbContext<ApplicationDbContext>(c => c.UseSqlite("Filename=DataBase.db"));
builder.Services.AddControllersWithViews().AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
builder.Services.AddSwaggerGen(c =>
{
// c.DocumentFilter<CustomModelDocumentFilter<Model>>();
c.MapType<Object>(() => new OpenApiSchema { Type = "object" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
builder.Services.AddSwaggerGenNewtonsoftSupport();
builder.Services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
builder.Services.AddIdentity<ApplicationUser, IdentityRole<int>>(options =>
{
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._";
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services.Configure<IdentityOptions>(opts =>
{
opts.Password.RequiredLength = 8;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = true;
opts.Password.RequireDigit = true;
});
// Adding Authentication
var authenticationBuilder = builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
});
// Adding Jwt Bearer
authenticationBuilder.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = AuthOptions.AUDIENCE,
ValidIssuer = AuthOptions.ISSUER,
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey()
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
if (context.Request.Headers.ContainsKey("Sec-WebSocket-Protocol") &&
context.HttpContext.WebSockets.IsWebSocketRequest)
{
var header = context.Request.Headers["Sec-WebSocket-Protocol"].ToString();
var newHeader = header;
foreach (var s in header.Split(','))
{
if (s.Trim().StartsWith("Bearer-"))
{
context.Token = s.Trim().Substring(7);
newHeader = String.Join(", ", newHeader.Split(",").Where(x => x != s).ToArray());
break;
}
}
context.Request.Headers["Sec-WebSocket-Protocol"] = newHeader;
}
return Task.CompletedTask;
},
OnTokenValidated = async context =>
{
var name = context.Principal.Claims.Where(x => x.Type == ClaimTypes.Name).FirstOrDefault().Value;
var userManager = context.HttpContext.RequestServices.GetService<UserManager<ApplicationUser>>();
var user = await userManager.FindByIdAsync(name);
if (user == null)
{
context.Fail("Invalid token");
}
else
{
context.HttpContext.Items["User"] = user;
}
}
};
});
// Google OAuth example
// authenticationBuilder.AddOAuth<GoogleOptions, ReplacedGoogleHandler>(GoogleDefaults.AuthenticationScheme, GoogleDefaults.DisplayName, googleOptions =>
// {
// googleOptions.ClientId = builder.Configuration["Authentication:Google:ClientId"];
// googleOptions.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
// googleOptions.CallbackPath = "/oauth/google";
//
// googleOptions.CorrelationCookie.Path = "/";
//
// googleOptions.Events.OnTicketReceived += ctx => AuthUtils.OnTicketReceived(ctx);
// });
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.UseAuthentication();
app.UseAuthorization();
app.Run();
public class CustomModelDocumentFilter<T> : IDocumentFilter where T : class
{
public void Apply(OpenApiDocument openapiDoc, DocumentFilterContext context)
{
context.SchemaGenerator.GenerateSchema(typeof(T), context.SchemaRepository);
}
}
public class AuthOptions
{
public const string ISSUER = "$rootnamespace$";
public const string AUDIENCE = "$rootnamespace$.Client";
public static string KEY = "";
public static SymmetricSecurityKey GetSymmetricSecurityKey() => new(Encoding.UTF8.GetBytes(KEY));
}