Guest User

Untitled

a guest
Dec 27th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using FoodNet.DataAccessCore;
  10. using Microsoft.EntityFrameworkCore;
  11. using Autofac;
  12. using FoodNET.WebAPI.Data;
  13. using FoodNET.WebAPI.Data.Interfaces;
  14. using Autofac.Extensions.DependencyInjection;
  15. using AutoMapper;
  16. using FoodNET.WebAPI.Data.DTOFacade;
  17. using IdentityServer4.Models;
  18. using IdentityServer4.Test;
  19. using System.Security.Claims;
  20. using IdentityModel;
  21. using IdentityServer4;
  22. using FoodNet.ModelCore.Domain;
  23. using System.Reflection;
  24. using Microsoft.AspNetCore.Identity;
  25.  
  26. namespace FoodNET.WebAPI
  27. {
  28.     public class Startup
  29.     {
  30.         public Startup(IConfiguration configuration)
  31.         {
  32.             Configuration = configuration;
  33.         }
  34.  
  35.         public IConfiguration Configuration { get; }
  36.  
  37.         // This method gets called by the runtime. Use this method to add services to the container.
  38.         public IServiceProvider ConfigureServices(IServiceCollection services)
  39.         {
  40.             var connectionString = Configuration.GetConnectionString("FoodNetDbContext");
  41.             var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
  42.  
  43.             services.AddDbContext<FoodNetDbContext>(opt => opt.UseSqlServer(connectionString));
  44.             services.AddDbContext<FoodNetDbContext>(efbuilder =>
  45.                 efbuilder.UseSqlServer(connectionString, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)));
  46.             services.AddIdentity<User, IdentityRole>()
  47.                 .AddEntityFrameworkStores<FoodNetDbContext>();
  48.  
  49.  
  50.             services.AddMvc();
  51.             services.AddAutoMapper();
  52.             services.AddIdentityServer()
  53.                         .AddInMemoryClients(Clients.Get())
  54.                         .AddInMemoryIdentityResources(Resources.GetIdentityResources())
  55.                         .AddInMemoryApiResources(Resources.GetApiResources())
  56.                         .AddAspNetIdentity<User>()
  57.                         .AddDeveloperSigningCredential()
  58.                         .AddOperationalStore(options =>
  59.                             options.ConfigureDbContext = efbuilder =>
  60.                                 efbuilder.UseSqlServer(connectionString,
  61.                                     sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)));
  62.  
  63.             // Autofac
  64.             var builder = new ContainerBuilder();
  65.  
  66.             builder.RegisterType<FridgeRepository>().As<IFridgeRepository>();
  67.             builder.RegisterType<ProductsRepository>().As<IProductsRepository>();
  68.             builder.RegisterType<RecipesRepository>().As<IRecipesRepository>();
  69.             builder.RegisterType<UsersRepository>().As<IUsersRepository>();
  70.             builder.RegisterType<ResultDataService>().As<IResultDataService>();
  71.  
  72.             builder.RegisterType<ProductsDTOFacade>().AsSelf();
  73.             builder.RegisterType<RecipesDTOFacade>().AsSelf();
  74.             builder.RegisterType<FridgesDTOFacade>().AsSelf();
  75.             builder.RegisterType<UsersDTOFacade>().AsSelf();
  76.             builder.RegisterType<ResultDataDTOFacade>().AsSelf();
  77.  
  78.             builder.Populate(services);
  79.             var container = builder.Build();
  80.             return new AutofacServiceProvider(container);
  81.         }
  82.  
  83.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  84.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  85.         {
  86.             if (env.IsDevelopment())
  87.             {
  88.                 app.UseDeveloperExceptionPage();
  89.             }
  90.  
  91.             app.UseIdentityServer();
  92.             app.UseAuthentication();
  93.             app.UseMvc();
  94.         }
  95.  
  96.         internal class Clients
  97.         {
  98.             public static IEnumerable<Client> Get()
  99.             {
  100.                 return new List<Client> {
  101.             new Client {
  102.                 ClientId = "oauthClient",
  103.                 ClientName = "Example Client Credentials Client Application",
  104.                 AllowedGrantTypes = GrantTypes.ClientCredentials,
  105.                 ClientSecrets = new List<Secret> {
  106.                     new Secret("superSecretPassword".Sha256())},
  107.                 AllowedScopes = new List<string> {"customAPI.read"}
  108.             },
  109.             new Client
  110.             {
  111.                     ClientId = "openIdConnectClient",
  112.                     ClientName = "FoodNET Client",
  113.                     AllowedGrantTypes = GrantTypes.Implicit,
  114.                     AllowedScopes = new List<string>
  115.                     {
  116.                         IdentityServerConstants.StandardScopes.OpenId,
  117.                         IdentityServerConstants.StandardScopes.Profile,
  118.                         IdentityServerConstants.StandardScopes.Email,
  119.                         "role",
  120.                         "customAPI.write"
  121.                     },
  122.                     RedirectUris = new List<string> {"https://localhost:44373/signin-oidc"},
  123.                     PostLogoutRedirectUris = new List<string> { "https://localhost:44373" }
  124.             }
  125.         };
  126.             }
  127.         }
  128.  
  129.         internal class Resources
  130.         {
  131.             public static IEnumerable<IdentityResource> GetIdentityResources()
  132.             {
  133.                 return new List<IdentityResource> {
  134.             new IdentityResources.OpenId(),
  135.             new IdentityResources.Profile(),
  136.             new IdentityResources.Email(),
  137.             new IdentityResource {
  138.                 Name = "role",
  139.                 UserClaims = new List<string> {"role"}
  140.             }
  141.         };
  142.             }
  143.  
  144.             public static IEnumerable<ApiResource> GetApiResources()
  145.             {
  146.                 return new List<ApiResource> {
  147.             new ApiResource {
  148.                 Name = "customAPI",
  149.                 DisplayName = "Custom API",
  150.                 Description = "Custom API Access",
  151.                 UserClaims = new List<string> {"role"},
  152.                 ApiSecrets = new List<Secret> {new Secret("scopeSecret".Sha256())},
  153.                 Scopes = new List<Scope> {
  154.                     new Scope("customAPI.read"),
  155.                     new Scope("customAPI.write")
  156.                 }
  157.             }
  158.         };
  159.             }
  160.         }
  161.  
  162.         internal class Users
  163.         {
  164.             public static List<TestUser> Get()
  165.             {
  166.                 return new List<TestUser> {
  167.             new TestUser {
  168.                 SubjectId = "5BE86359-073C-434B-AD2D-A3932222DABE",
  169.                 Username = "scott",
  170.                 Password = "password",
  171.                 Claims = new List<Claim> {
  172.                     new Claim(JwtClaimTypes.Email, "scott@scottbrady91.com"),
  173.                     new Claim(JwtClaimTypes.Role, "admin")
  174.                 }
  175.             }
  176.         };
  177.             }
  178.         }
  179.     }
  180.  
  181. }
Add Comment
Please, Sign In to add comment