150 lines
4.7 KiB
C#
150 lines
4.7 KiB
C#
using System.Text.Json.Serialization;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.FluentUI.AspNetCore.Components;
|
|
using Microsoft.OpenApi.Models;
|
|
using Raven.Client.Documents;
|
|
using Raven.DependencyInjection;
|
|
using Raven.Identity;
|
|
using Swashbuckle.AspNetCore.Filters;
|
|
using The_Metaverse_Engine.Handlers;
|
|
using The_Metaverse_Engine.Services;
|
|
|
|
AppContext.SetSwitch("Microsoft.AspNetCore.Authentication.SuppressAutoDefaultScheme", false);
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var handler = new EventHandler<UnobservedTaskExceptionEventArgs>((s, e) => throw e.Exception);
|
|
TaskScheduler.UnobservedTaskException += handler;
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers().AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
});
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen(o =>
|
|
{
|
|
var sc = new OpenApiSecurityScheme
|
|
{
|
|
In = ParameterLocation.Query,
|
|
Name = "apikey",
|
|
Type = SecuritySchemeType.ApiKey,
|
|
Description = "Enter API key",
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "apikey"
|
|
}
|
|
};
|
|
o.AddSecurityDefinition(sc.Reference.Id, sc);
|
|
|
|
//to indicate the entire API is secured, add this. NOTE it does NOT secure it, just indicates it is.
|
|
// o.AddSecurityRequirement(new OpenApiSecurityRequirement { { sc, new string[] { } } });
|
|
o.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
|
|
//this filter does add if an API is secured based upon the Authorize attribute
|
|
o.OperationFilter<SecurityRequirementsOperationFilter>(true, sc.Reference.Id);
|
|
}
|
|
);
|
|
|
|
// Create an IDocumentStore singleton from the RavenSettings.
|
|
builder.Services.AddRavenDbDocStore();
|
|
// Create a RavenDB IAsyncDocumentSession for each request. You're responsible for calling .SaveChanges after each request.
|
|
builder.Services.AddRavenDbSession();
|
|
builder.Services.AddRavenDbAsyncSession();
|
|
// Adds an identity system to ASP.NET Core
|
|
builder.Services.AddIdentity<AppUser, Raven.Identity.IdentityRole>()
|
|
// Use RavenDB as the store for identity users and roles. Specify your app user type here, and your role type. If you don't have a role type, use Raven.Identity.IdentityRole.
|
|
.AddRavenDbIdentityStores<AppUser, Raven.Identity.IdentityRole>()
|
|
.AddTokenProvider<DataProtectorTokenProvider<AppUser>>(TokenOptions.DefaultProvider)
|
|
.AddDefaultUI();
|
|
|
|
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<AppUser>>();
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
policy.WithOrigins("*");
|
|
policy.AllowAnyHeader();
|
|
policy.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
builder.Services.AddStackExchangeRedisCache(options =>
|
|
{
|
|
options.Configuration = builder.Configuration.GetConnectionString("Redis");
|
|
options.InstanceName = "tme";
|
|
});
|
|
|
|
builder.Services.AddAuthentication().AddJwtBearer();
|
|
|
|
builder.Services.AddAuthorization(options =>
|
|
{
|
|
options.AddPolicy("ApiKey", policy =>
|
|
{
|
|
policy.AddAuthenticationSchemes([JwtBearerDefaults.AuthenticationScheme]);
|
|
policy.Requirements.Add(new ApiKeyRequirement());
|
|
});
|
|
|
|
options.FallbackPolicy = new AuthorizationPolicyBuilder()
|
|
.RequireAuthenticatedUser()
|
|
.Build();
|
|
});
|
|
|
|
builder.Services.AddScoped<IAuthorizationHandler, ApiKeyHandler>();
|
|
|
|
builder.Services.AddFluentUIComponents();
|
|
|
|
builder.Services.AddHttpClient();
|
|
builder.WebHost.UseStaticWebAssets();
|
|
|
|
builder.Services.AddSingleton<Config>();
|
|
|
|
builder.Services.AddRazorPages();
|
|
builder.Services.AddServerSideBlazor().AddCircuitOptions(option =>
|
|
{
|
|
option.DetailedErrors = builder.Environment.IsDevelopment();
|
|
});;
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
// using (var scope = app.Services.CreateScope())
|
|
// {
|
|
// var db = scope.ServiceProvider.GetRequiredService<IDocumentStore>();
|
|
// new Rank_ById().Execute(db);
|
|
// }
|
|
|
|
app.UseCors(); // UseCors must be called before MapHub.
|
|
|
|
// Configure the HTTP request pipeline.
|
|
#if DEBUG
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
#endif
|
|
|
|
// app.MapHub<HypetrainBroadcaster>("/hypetrain");
|
|
|
|
app.UseStaticFiles();
|
|
|
|
// app.UseHttpsRedirection();
|
|
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.MapControllers();
|
|
app.MapGet("/api/ok", () => "OK").AllowAnonymous();
|
|
|
|
app.MapBlazorHub();
|
|
app.MapFallbackToPage("/_Host");
|
|
|
|
app.Run();
|