Advertisement
kirilstanoev

Untitled

Jun 1st, 2021
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Identity;
  5. using Microsoft.AspNetCore.Identity.UI.Services;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.DependencyInjection.Extensions;
  10. using Microsoft.Extensions.Hosting;
  11. using Microsoft.IdentityModel.Tokens;
  12. using Microsoft.OpenApi.Models;
  13.  
  14. using Newtonsoft.Json;
  15.  
  16. using PhotoContest.Data;
  17. using PhotoContest.Data.Models;
  18. using PhotoContest.Services.Contracts;
  19. using PhotoContest.Services.Contracts.SecurityContracts;
  20. using PhotoContest.Services.Models.SecurityModels;
  21. using PhotoContest.Services.Services;
  22. using PhotoContest.Services.Services.BackgroundTask;
  23. using PhotoContest.Services.Services.SecurityService;
  24.  
  25. using System;
  26. using System.IO;
  27. using System.Reflection;
  28. using System.Text;
  29.  
  30. namespace PhotoContest.Web
  31. {
  32.     public class Startup
  33.     {
  34.         public Startup(IConfiguration configuration)
  35.         {
  36.             Configuration = configuration;
  37.         }
  38.  
  39.         public IConfiguration Configuration { get; }
  40.  
  41.         // This method gets called by the runtime. Use this method to add services to the container.
  42.         public void ConfigureServices(IServiceCollection services)
  43.         {
  44.             services.AddControllersWithViews()
  45.                     .AddNewtonsoftJson(options =>
  46.                     {
  47.                         options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  48.                     });
  49.  
  50.             services.AddRazorPages();
  51.  
  52.             services.Configure<JWT>(Configuration.GetSection("JWT"));
  53.  
  54.             services.AddSwaggerGen(c =>
  55.             {
  56.                 c.SwaggerDoc("PhotoContest", new OpenApiInfo { Title = "PhotoContest" });
  57.  
  58.                 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  59.                 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
  60.                 c.IncludeXmlComments(xmlPath);
  61.  
  62.                 var securitySchema = new OpenApiSecurityScheme
  63.                 {
  64.                     Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
  65.                     Name = "Authorization",
  66.                     In = ParameterLocation.Header,
  67.                     Type = SecuritySchemeType.Http,
  68.                     Scheme = "bearer",
  69.                     Reference = new OpenApiReference
  70.                     {
  71.                         Type = ReferenceType.SecurityScheme,
  72.                         Id = "Bearer"
  73.                     }
  74.                 };
  75.                 c.AddSecurityDefinition("Bearer", securitySchema);
  76.  
  77.                 var securityRequirement = new OpenApiSecurityRequirement
  78.                 {
  79.                     { securitySchema, new[] { "Bearer" } }
  80.                 };
  81.  
  82.                 c.AddSecurityRequirement(securityRequirement);
  83.             });
  84.  
  85.             services.AddDbContext<PhotoContestContext>(options =>
  86.             {
  87.                 options.UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection"));
  88.             });
  89.  
  90.             services
  91.                 .AddDefaultIdentity<User>
  92.                     (options =>
  93.                     {
  94.                         options.SignIn.RequireConfirmedAccount = false;
  95.                         options.Password.RequireNonAlphanumeric = false;
  96.                         options.Password.RequireUppercase = false;
  97.                         options.Password.RequiredLength = 8;
  98.                         options.Password.RequireLowercase = false;
  99.                     })
  100.                 .AddRoles<Role>()
  101.                 .AddEntityFrameworkStores<PhotoContestContext>();
  102.  
  103.             //
  104.             // services
  105.             //  .AddIdentity<User, Role>(options =>
  106.             //  {
  107.             //      options.SignIn.RequireConfirmedAccount = false;
  108.             //      options.Password.RequireNonAlphanumeric = false;
  109.             //      options.Password.RequireUppercase = false;
  110.             //      options.Password.RequiredLength = 8;
  111.             //      options.Password.RequireLowercase = false;
  112.             //  })
  113.             //  .AddEntityFrameworkStores<PhotoContestContext>()
  114.             //  .AddDefaultTokenProviders();
  115.             //
  116.  
  117.             services.AddScoped<ICategoryService, CategoryService>();
  118.             services.AddScoped<IPhotoService, PhotoService>();
  119.             services.AddScoped<IUserService, UserService>();
  120.             services.AddScoped<ITokenService, TokenService>();
  121.             services.AddScoped<IReviewService, ReviewService>();
  122.             services.AddScoped<IContestService, ContestService>();
  123.             services.AddScoped<IUserContestService, UserContestService>();
  124.             services.AddSingleton<IEmailSender, EmailSender>();
  125.  
  126.             services.AddHostedService<TimedHostedService>();
  127.             services.AddAutoMapper(typeof(Startup));
  128.  
  129.             //
  130.             // services.TryAddScoped<SignInManager<User>>();
  131.             //
  132.  
  133.  
  134.             // ----------------------------------------------------------------------------------------------
  135.             //services
  136.             //  .AddAuthentication(options =>
  137.             //  {
  138.             //      options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  139.             //      options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  140.             //  })
  141.             //  .AddJwtBearer(options =>
  142.             //  {
  143.             //      //options.RequireHttpsMetadata = false;
  144.             //      //options.SaveToken = false;
  145.             //      options.TokenValidationParameters = new TokenValidationParameters
  146.             //      {
  147.             //          ValidateIssuerSigningKey = true,
  148.             //          ValidateIssuer = true,
  149.             //          ValidateAudience = true,
  150.             //          ValidateLifetime = true,
  151.             //          ClockSkew = TimeSpan.Zero,
  152.             //          ValidIssuer = Configuration["JWT:Issuer"],
  153.             //          ValidAudience = Configuration["JWT:Audience"],
  154.             //          IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Key"]))
  155.             //      };
  156.             //  });
  157.             // -------------------------------------------------------------------------------------------------
  158.  
  159.  
  160.  
  161.         }
  162.  
  163.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  164.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  165.         {
  166.             if (env.IsDevelopment())
  167.             {
  168.                 app.UseDeveloperExceptionPage();
  169.                 app.UseSwaggerUI(c =>
  170.                 {
  171.                     c.SwaggerEndpoint("/swagger/PhotoContest/swagger.json", "PhotoContest");
  172.                 });
  173.                 app.UseSwagger();
  174.             }
  175.             else
  176.             {
  177.                 app.UseExceptionHandler("/Home/Error");
  178.             }
  179.             app.UseStaticFiles();
  180.  
  181.             app.UseRouting();
  182.             app.UseAuthentication();
  183.             app.UseAuthorization();
  184.  
  185.             app.UseEndpoints(endpoints =>
  186.             {
  187.                 endpoints.MapControllerRoute(
  188.                     name: "default",
  189.                     pattern: "{controller=Home}/{action=Index}/{id?}");
  190.                 endpoints.MapRazorPages();
  191.             });
  192.         }
  193.     }
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement