Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using FoodNet.DataAccessCore;
- using Microsoft.EntityFrameworkCore;
- using Autofac;
- using FoodNET.WebAPI.Data;
- using FoodNET.WebAPI.Data.Interfaces;
- using Autofac.Extensions.DependencyInjection;
- using AutoMapper;
- using FoodNET.WebAPI.Data.DTOFacade;
- using IdentityServer4.Models;
- using IdentityServer4.Test;
- using System.Security.Claims;
- using IdentityModel;
- using IdentityServer4;
- using FoodNet.ModelCore.Domain;
- using System.Reflection;
- using Microsoft.AspNetCore.Identity;
- namespace FoodNET.WebAPI
- {
- 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 IServiceProvider ConfigureServices(IServiceCollection services)
- {
- var connectionString = Configuration.GetConnectionString("FoodNetDbContext");
- var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
- services.AddDbContext<FoodNetDbContext>(opt => opt.UseSqlServer(connectionString));
- services.AddDbContext<FoodNetDbContext>(efbuilder =>
- efbuilder.UseSqlServer(connectionString, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)));
- services.AddIdentity<User, IdentityRole>()
- .AddEntityFrameworkStores<FoodNetDbContext>();
- services.AddMvc();
- services.AddAutoMapper();
- services.AddIdentityServer()
- .AddInMemoryClients(Clients.Get())
- .AddInMemoryIdentityResources(Resources.GetIdentityResources())
- .AddInMemoryApiResources(Resources.GetApiResources())
- .AddAspNetIdentity<User>()
- .AddDeveloperSigningCredential()
- .AddOperationalStore(options =>
- options.ConfigureDbContext = efbuilder =>
- efbuilder.UseSqlServer(connectionString,
- sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)));
- // Autofac
- var builder = new ContainerBuilder();
- builder.RegisterType<FridgeRepository>().As<IFridgeRepository>();
- builder.RegisterType<ProductsRepository>().As<IProductsRepository>();
- builder.RegisterType<RecipesRepository>().As<IRecipesRepository>();
- builder.RegisterType<UsersRepository>().As<IUsersRepository>();
- builder.RegisterType<ResultDataService>().As<IResultDataService>();
- builder.RegisterType<ProductsDTOFacade>().AsSelf();
- builder.RegisterType<RecipesDTOFacade>().AsSelf();
- builder.RegisterType<FridgesDTOFacade>().AsSelf();
- builder.RegisterType<UsersDTOFacade>().AsSelf();
- builder.RegisterType<ResultDataDTOFacade>().AsSelf();
- builder.Populate(services);
- var container = builder.Build();
- return new AutofacServiceProvider(container);
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseIdentityServer();
- app.UseAuthentication();
- app.UseMvc();
- }
- internal class Clients
- {
- public static IEnumerable<Client> Get()
- {
- return new List<Client> {
- new Client {
- ClientId = "oauthClient",
- ClientName = "Example Client Credentials Client Application",
- AllowedGrantTypes = GrantTypes.ClientCredentials,
- ClientSecrets = new List<Secret> {
- new Secret("superSecretPassword".Sha256())},
- AllowedScopes = new List<string> {"customAPI.read"}
- },
- new Client
- {
- ClientId = "openIdConnectClient",
- ClientName = "FoodNET Client",
- AllowedGrantTypes = GrantTypes.Implicit,
- AllowedScopes = new List<string>
- {
- IdentityServerConstants.StandardScopes.OpenId,
- IdentityServerConstants.StandardScopes.Profile,
- IdentityServerConstants.StandardScopes.Email,
- "role",
- "customAPI.write"
- },
- RedirectUris = new List<string> {"https://localhost:44373/signin-oidc"},
- PostLogoutRedirectUris = new List<string> { "https://localhost:44373" }
- }
- };
- }
- }
- internal class Resources
- {
- public static IEnumerable<IdentityResource> GetIdentityResources()
- {
- return new List<IdentityResource> {
- new IdentityResources.OpenId(),
- new IdentityResources.Profile(),
- new IdentityResources.Email(),
- new IdentityResource {
- Name = "role",
- UserClaims = new List<string> {"role"}
- }
- };
- }
- public static IEnumerable<ApiResource> GetApiResources()
- {
- return new List<ApiResource> {
- new ApiResource {
- Name = "customAPI",
- DisplayName = "Custom API",
- Description = "Custom API Access",
- UserClaims = new List<string> {"role"},
- ApiSecrets = new List<Secret> {new Secret("scopeSecret".Sha256())},
- Scopes = new List<Scope> {
- new Scope("customAPI.read"),
- new Scope("customAPI.write")
- }
- }
- };
- }
- }
- internal class Users
- {
- public static List<TestUser> Get()
- {
- return new List<TestUser> {
- new TestUser {
- SubjectId = "5BE86359-073C-434B-AD2D-A3932222DABE",
- Username = "scott",
- Password = "password",
- Claims = new List<Claim> {
- new Claim(JwtClaimTypes.Email, "scott@scottbrady91.com"),
- new Claim(JwtClaimTypes.Role, "admin")
- }
- }
- };
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment