Advertisement
Guest User

Untitled

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