Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Identity.UI.Services;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using Microsoft.Extensions.Hosting;
- using Microsoft.IdentityModel.Tokens;
- using Microsoft.OpenApi.Models;
- using Newtonsoft.Json;
- using PhotoContest.Data;
- using PhotoContest.Data.Models;
- using PhotoContest.Services.Contracts;
- using PhotoContest.Services.Contracts.SecurityContracts;
- using PhotoContest.Services.Models.SecurityModels;
- using PhotoContest.Services.Services;
- using PhotoContest.Services.Services.BackgroundTask;
- using PhotoContest.Services.Services.SecurityService;
- using System;
- using System.IO;
- using System.Reflection;
- using System.Text;
- namespace PhotoContest.Web
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllersWithViews()
- .AddNewtonsoftJson(options =>
- {
- options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
- });
- services.AddRazorPages();
- services.Configure<JWT>(Configuration.GetSection("JWT"));
- services.AddSwaggerGen(c =>
- {
- c.SwaggerDoc("PhotoContest", new OpenApiInfo { Title = "PhotoContest" });
- var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
- var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
- c.IncludeXmlComments(xmlPath);
- var securitySchema = new OpenApiSecurityScheme
- {
- Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
- Name = "Authorization",
- In = ParameterLocation.Header,
- Type = SecuritySchemeType.Http,
- Scheme = "bearer",
- Reference = new OpenApiReference
- {
- Type = ReferenceType.SecurityScheme,
- Id = "Bearer"
- }
- };
- c.AddSecurityDefinition("Bearer", securitySchema);
- var securityRequirement = new OpenApiSecurityRequirement
- {
- { securitySchema, new[] { "Bearer" } }
- };
- c.AddSecurityRequirement(securityRequirement);
- });
- services.AddDbContext<PhotoContestContext>(options =>
- {
- options.UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection"));
- });
- services
- .AddDefaultIdentity<User>
- (options =>
- {
- options.SignIn.RequireConfirmedAccount = false;
- options.Password.RequireNonAlphanumeric = false;
- options.Password.RequireUppercase = false;
- options.Password.RequiredLength = 8;
- options.Password.RequireLowercase = false;
- })
- .AddRoles<Role>()
- .AddEntityFrameworkStores<PhotoContestContext>();
- //
- // services
- // .AddIdentity<User, Role>(options =>
- // {
- // options.SignIn.RequireConfirmedAccount = false;
- // options.Password.RequireNonAlphanumeric = false;
- // options.Password.RequireUppercase = false;
- // options.Password.RequiredLength = 8;
- // options.Password.RequireLowercase = false;
- // })
- // .AddEntityFrameworkStores<PhotoContestContext>()
- // .AddDefaultTokenProviders();
- //
- services.AddScoped<ICategoryService, CategoryService>();
- services.AddScoped<IPhotoService, PhotoService>();
- services.AddScoped<IUserService, UserService>();
- services.AddScoped<ITokenService, TokenService>();
- services.AddScoped<IReviewService, ReviewService>();
- services.AddScoped<IContestService, ContestService>();
- services.AddScoped<IUserContestService, UserContestService>();
- services.AddSingleton<IEmailSender, EmailSender>();
- services.AddHostedService<TimedHostedService>();
- services.AddAutoMapper(typeof(Startup));
- //
- // services.TryAddScoped<SignInManager<User>>();
- //
- // ----------------------------------------------------------------------------------------------
- //services
- // .AddAuthentication(options =>
- // {
- // options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- // options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- // })
- // .AddJwtBearer(options =>
- // {
- // //options.RequireHttpsMetadata = false;
- // //options.SaveToken = false;
- // options.TokenValidationParameters = new TokenValidationParameters
- // {
- // ValidateIssuerSigningKey = true,
- // ValidateIssuer = true,
- // ValidateAudience = true,
- // ValidateLifetime = true,
- // ClockSkew = TimeSpan.Zero,
- // ValidIssuer = Configuration["JWT:Issuer"],
- // ValidAudience = Configuration["JWT:Audience"],
- // IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Key"]))
- // };
- // });
- // -------------------------------------------------------------------------------------------------
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseSwaggerUI(c =>
- {
- c.SwaggerEndpoint("/swagger/PhotoContest/swagger.json", "PhotoContest");
- });
- app.UseSwagger();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles();
- app.UseRouting();
- app.UseAuthentication();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- endpoints.MapRazorPages();
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement