public void TestKey() { var d = new ECDsaCng(); d.HashAlgorithm = CngAlgorithm.Sha256; Console.WriteLine(d.Key.Export(CngKeyBlobFormat.EccPublicBlob)); X509Certificate2 cert = new X509Certificate2(); ECDsaCertificateExtensions.GetECDsaPublicKey(cert); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddLogging(); // TODO: add real certificate var certificate = new X509Certificate2("certs/dev.pfx"); var certificateKey = new X509SecurityKey(certificate); var securityKey = new ECDsaSecurityKey(ECDsaCertificateExtensions.GetECDsaPrivateKey(certificate)); services.AddAuthentication(opt => { opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(opt => { opt.RequireHttpsMetadata = false; opt.SaveToken = true; opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = securityKey, ValidateIssuer = false, ValidateAudience = false, }; }).AddScheme <Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, JudgerAuthenticateMiddleware>("judger", null); services.AddAuthorization(opt => { opt.AddPolicy("user", policy => policy.RequireRole("User", "Admin", "Root")); opt.AddPolicy("admin", policy => policy.RequireRole("Admin", "Root")); opt.AddPolicy("root", policy => policy.RequireRole("Root")); opt.AddPolicy("judger", policy => policy.RequireRole("judger").AddAuthenticationSchemes("judger")); }); services.AddSingleton <Models.Auth.AuthInfo>(_ => new Models.Auth.AuthInfo { SigningKey = securityKey }); // Setup database stuff var pgsqlLinkParams = Configuration.GetValue <string>("pgsqlLink"); var alwaysMigrate = Configuration.GetValue <bool>("alwaysMigrate"); services.AddSingleton(_ => new DbOptions { AlwaysMigrate = alwaysMigrate }); var testStorageParams = new SingleBucketFileStorageService.Params(); Configuration.GetSection("testStorage").Bind(testStorageParams); services.AddDbContextPool <Models.RurikawaDb>(options => { options.UseNpgsql(pgsqlLinkParams); }); // Setup redis var redisConnString = Configuration.GetValue <string>("redisLink"); services.AddSingleton(_ => new RedisService(redisConnString)); services.AddSingleton( svc => new SingleBucketFileStorageService( testStorageParams, svc.GetService <ILogger <SingleBucketFileStorageService> >()) ); services.AddSingleton <JudgerCoordinatorService>(); services.AddSingleton <FrontendUpdateService>(); services.AddScoped <AccountService>(); services.AddScoped <JudgerService>(); services.AddScoped <ProfileService>(); services.AddScoped <DbService>(); services.AddSingleton <JudgerAuthenticateService>(); services.AddSingleton <DbVacuumingService>(); services.AddSingleton <JsonSerializerOptions>(_ => SetupJsonSerializerOptions(new JsonSerializerOptions()) ); services.AddSingleton <GenericCacheService>(); services.AddSingleton <RurikawaCacheService>(); services.AddSwaggerDocument(); services.AddRouting(options => { options.LowercaseUrls = true; }); services.AddControllers().AddJsonOptions(opt => SetupJsonSerializerOptions(opt.JsonSerializerOptions)); }