Advertisement
Guest User

Startup

a guest
Nov 21st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.AspNetCore.Authentication.JwtBearer;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.IdentityModel.Tokens;
  11. using Newtonsoft.Json;
  12. using Oliver.PetShop.Core.ApplicationService;
  13. using Oliver.PetShop.Core.ApplicationService.impl;
  14. using Oliver.PetShop.Core.DomainService;
  15. using Oliver.PetShop.Core.Entity;
  16. using Oliver.PetShop.Infrastructure.Data2;
  17. using Oliver.PetShop.Infrastructure.Data2.Repositories;
  18. using Oliver.PetShop.UI.PetRestAPI.Helpers;
  19.  
  20. namespace Oliver.PetShop.UI.PetRestAPI
  21. {
  22.     public class Startup
  23.     {
  24.         public Startup(IConfiguration configuration, IHostingEnvironment env)
  25.         {
  26.             Configuration = configuration;
  27.             Environment = env;
  28.             JwtSecurityKey.SetSecret("a secret that needs to be at least 16 characters long");
  29.         }
  30.  
  31.         public IConfiguration Configuration { get; }
  32.         public IHostingEnvironment Environment { get; }
  33.  
  34.         // This method gets called by the runtime. Use this method to add services to the container.
  35.         public void ConfigureServices(IServiceCollection services)
  36.         {
  37.             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
  38.             {
  39.                 options.TokenValidationParameters = new TokenValidationParameters
  40.                 {
  41.                     ValidateAudience = false,
  42.                     //ValidAudience = "TodoApiClient",
  43.                     ValidateIssuer = false,
  44.                     //ValidIssuer = "TodoApi",
  45.                     ValidateIssuerSigningKey = true,
  46.                     IssuerSigningKey = JwtSecurityKey.Key,
  47.                     ValidateLifetime = true, //validate the expiration and not before values in the token
  48.                     ClockSkew = TimeSpan.FromMinutes(5) //5 minute tolerance for the expiration date
  49.                 };
  50.             });
  51.  
  52.             services.AddCors();
  53.  
  54.             services.AddDbContext<PetShopAppContext>(opt => opt.UseInMemoryDatabase("PetShop9876"));
  55.  
  56.             services.AddScoped<IPetService, PetService>();
  57.             services.AddScoped<IPetRepository, PetRepository>();
  58.             services.AddScoped<IOwnerService, OwnerService>();
  59.             services.AddScoped<IOwnerRepository, OwnerRepository>();
  60.  
  61.             services.AddMvc().AddJsonOptions(option =>
  62.             {
  63.                 option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  64.             });
  65.  
  66.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  67.         }
  68.  
  69.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  70.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  71.         {
  72.             if (env.IsDevelopment())
  73.             {
  74.                 app.UseDeveloperExceptionPage();
  75.                 using (var scope = app.ApplicationServices.CreateScope())
  76.                 {
  77.                     var ctx = scope.ServiceProvider.GetService<PetShopAppContext>();
  78.  
  79.                     string password = "1234";
  80.                     byte[] passwordHashJoe, passwordSaltJoe, passwordHashOle, passwordSaltOle;
  81.                     CreatePasswordHash(password, out passwordHashJoe, out passwordSaltJoe);
  82.                     CreatePasswordHash(password, out passwordHashOle, out passwordSaltOle);
  83.  
  84.                     var owner1 = ctx.Owners.Add(new Owner()
  85.                     {
  86.                         Id = 1,
  87.                         Name = "Børge"
  88.                     }).Entity;
  89.  
  90.                     var owner2 = ctx.Owners.Add(new Owner()
  91.                     {
  92.                         Id = 2,
  93.                         Name = "Hans"
  94.                     }).Entity;
  95.  
  96.                     var pet1 = ctx.Pets.Add(new Pet()
  97.                     {
  98.                         Id = 1,
  99.                         Name = "Jens",
  100.                         Type = "Hund",
  101.                         Color = "Blå",
  102.                         PreviousOwner = owner1,
  103.                         Price = 456
  104.                     }).Entity;
  105.  
  106.                     var pet2 = ctx.Pets.Add(new Pet()
  107.                     {
  108.                         Id = 2,
  109.                         Name = "Ole",
  110.                         Type = "Kat",
  111.                         Color = "Rød",
  112.                         PreviousOwner = owner2,
  113.                         Price = 489
  114.                     });
  115.  
  116.                     List<User> users = new List<User>
  117.                     {
  118.                         new User
  119.                         {
  120.                             Id = 1,
  121.                         Username = "UserJoe",
  122.                         PasswordHash = passwordHashJoe,
  123.                         PasswordSalt = passwordSaltJoe,
  124.                         IsAdmin = false
  125.                         },
  126.                         new User
  127.                         {
  128.                             Id = 2,
  129.                         Username = "UserOle",
  130.                         PasswordHash = passwordHashOle,
  131.                         PasswordSalt = passwordSaltOle,
  132.                         IsAdmin = true
  133.                         }
  134.                     };
  135.  
  136.                     ctx.Users.AddRange(users);
  137.                     ctx.SaveChanges();
  138.                 }
  139.             }
  140.             else
  141.             {
  142.                 app.UseHsts();
  143.             }
  144.  
  145.             app.UseHttpsRedirection();
  146.             app.UseMvc();
  147.         }
  148.  
  149.         private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
  150.         {
  151.             using (var hmac = new System.Security.Cryptography.HMACSHA512())
  152.             {
  153.                 passwordSalt = hmac.Key;
  154.                 passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
  155.             }
  156.         }
  157.  
  158.       }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement