hanyadikhetvan-dotnethf/HanyadikHetVan/Startup.cs

177 lines
7.0 KiB
C#
Raw Normal View History

2021-05-03 01:30:06 +02:00
using HanyadikHetVan.Data;
2021-05-19 17:56:56 +02:00
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.Infrastructure;
2021-05-03 15:56:38 +02:00
using HanyadikHetVan.Services;
2021-05-19 17:56:56 +02:00
using IdentityServer4.Services;
2021-05-03 01:30:06 +02:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
2021-05-19 23:14:57 +02:00
using Microsoft.AspNetCore.Identity.UI.Services;
2021-05-03 01:30:06 +02:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2021-05-19 23:14:57 +02:00
using Microsoft.IdentityModel.Tokens;
2021-05-03 18:15:26 +02:00
using Microsoft.OpenApi.Models;
2021-05-19 23:14:57 +02:00
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
2021-05-19 04:33:53 +02:00
using System.Reflection;
2021-05-03 01:30:06 +02:00
namespace HanyadikHetVan
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
2021-05-19 04:33:53 +02:00
services.AddAutoMapper(Assembly.GetExecutingAssembly());
2021-05-03 18:15:26 +02:00
services.AddTransient<WeeklyTimeSpanService>();
services.AddTransient<PauseService>();
2021-05-20 01:36:54 +02:00
services.AddTransient<UserService>();
2021-05-19 17:56:56 +02:00
services.AddTransient<FunFactService>();
2021-05-19 04:33:53 +02:00
services.AddTransient<HanyadikHetVanEntityService>();
2021-05-03 15:56:38 +02:00
services.AddTransient<HanyadikHetVanJsonService>();
2021-05-19 17:56:56 +02:00
services.AddTransient<IdentityService>();
2021-05-03 15:56:38 +02:00
services.AddTransient<HanyadikHetVanService>();
2021-05-19 17:56:56 +02:00
services.AddTransient<IProfileService, ProfileService>();
2021-05-19 23:14:57 +02:00
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<EmailSenderConfig>(Configuration.GetSection("EmailSender"));
2021-05-03 01:30:06 +02:00
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddAuthentication(config =>
{
config.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(config =>
{
config.Authority = "https://localhost:5001";
config.Audience = "api";
config.RequireHttpsMetadata = false;
2021-05-19 23:14:57 +02:00
config.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = true,
ValidateIssuerSigningKey = false,
RequireSignedTokens = false,
SignatureValidator = delegate (string token, TokenValidationParameters parameters)
{
return new JwtSecurityToken(token);
},
ValidateLifetime = false,
RequireExpirationTime = false
};
2021-05-03 01:30:06 +02:00
});
services.AddAuthorization(config =>
{
config.AddPolicy("default", config => config.RequireAuthenticatedUser().AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
config.DefaultPolicy = config.GetPolicy("default");
2021-05-19 23:14:57 +02:00
config.AddPolicy("AdminPolicy", p => p.RequireClaim("user_role", "admin"));
2021-05-03 01:30:06 +02:00
});
2021-05-19 23:14:57 +02:00
services.AddIdentity<User, IdentityRole>(options => { options.SignIn.RequireConfirmedAccount = true; })
2021-05-19 17:56:56 +02:00
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
2021-05-19 23:14:57 +02:00
services.AddIdentityServer(o =>
{
o.UserInteraction.LoginUrl = "/Identity/Account/Login";
o.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
o.UserInteraction.ErrorUrl = "/Identity/Account/Error";
})
2021-05-03 01:30:06 +02:00
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Configuration.GetSection("IdentityServer:IdentityResources"))
.AddInMemoryApiResources(Configuration.GetSection("IdentityServer:ApiResources"))
.AddInMemoryApiScopes(Configuration.GetSection("IdentityServer:ApiScopes"))
.AddInMemoryClients(Configuration.GetSection("IdentityServer:Clients"))
2021-05-19 23:14:57 +02:00
.AddAspNetIdentity<User>()
.AddProfileService<ProfileService>();
2021-05-03 01:30:06 +02:00
services.AddRazorPages();
services.AddControllers();
2021-05-03 18:15:26 +02:00
2021-05-19 04:33:53 +02:00
services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
2021-05-03 18:15:26 +02:00
services.AddSwaggerGen(c =>
{
2021-05-19 23:14:57 +02:00
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("https://localhost:5001/connect/authorize"),
TokenUrl = new Uri("https://localhost:5001/connect/token"),
Scopes = new Dictionary<string, string>
{
{"profile", "Profile"},
{"openid", "OpenID"},
{"user_role", "User roles"},
{"api.readwrite", "Access to api"}
}
}
}
});
2021-05-03 18:15:26 +02:00
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Hanyadik Het Van API", Version = "v1" });
2021-05-19 04:33:53 +02:00
c.SwaggerDoc("v2", new OpenApiInfo { Title = "Hanyadik Het Van API", Version = "v2" });
2021-05-03 18:15:26 +02:00
});
2021-05-03 01:30:06 +02:00
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
2021-05-03 18:15:26 +02:00
app.UseSwagger();
2021-05-19 04:33:53 +02:00
app.UseSwaggerUI(c =>
2021-05-19 23:14:57 +02:00
{
c.SwaggerEndpoint("v1/swagger.json", "Original API");
c.SwaggerEndpoint("v2/swagger.json", "Homework API");
});
2021-05-03 01:30:06 +02:00
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseIdentityServer();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}
}
}