private static void InitializeIdentityServer(IServiceProvider provider) { var context = provider.GetRequiredService <ConfigurationDbContext>(); if (!context.Clients.Any()) { foreach (var client in IdentityServerConfiguration.GetClients()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } if (!context.IdentityResources.Any()) { foreach (var resource in IdentityServerConfiguration.GetIdentityResources()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } if (!context.ApiResources.Any()) { foreach (var resource in IdentityServerConfiguration.GetApis()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddIdentityServer() .AddInMemoryIdentityResources(IdentityServerConfiguration.GetIdentityResources()) .AddInMemoryApiResources(IdentityServerConfiguration.GetApis()) .AddInMemoryClients(IdentityServerConfiguration.GetClients()) .AddTestUsers(IdentityServerConfiguration.GetUsers()) .AddDeveloperSigningCredential(); }
/// <summary> /// Configures the services. /// </summary> /// <param name="services">The services.</param> public void ConfigureServices(IServiceCollection services) { /*services.AddIdentity<ApplicationUser, IdentityRole>() * .AddEntityFrameworkStores<ApplicationDbContext>() * .AddDefaultTokenProviders();*/ services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryApiResources(IdentityServerConfiguration.GetApis()) .AddInMemoryClients(IdentityServerConfiguration.GetClients()); services.AddControllersWithViews(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UseSqlServerStorage(Configuration.GetConnectionString("SqlConnection"), new SqlServerStorageOptions { CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), QueuePollInterval = TimeSpan.Zero, UseRecommendedIsolationLevel = true, DisableGlobalLocks = true })); services.AddHangfireServer(); services.AddCors(options => { options.AddPolicy(name: AllowSpecificOrigins, builder => { builder.WithOrigins("http://localhost:3000") .AllowAnyHeader() .AllowAnyMethod(); }); }); services.AddSignalR(); services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore );; services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlConnection"))); //Dev db and real db are different Reminder services.AddScoped <IAuthorizationHandler, PermissionHandler>(); services.AddSimpleInjector(container, options => { options.AddAspNetCore() .AddControllerActivation(); }); services.AddIdentity <User, Roles>(options => { options.SignIn.RequireConfirmedAccount = true; options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider; }).AddDefaultTokenProviders() .AddEntityFrameworkStores <ApplicationDbContext>(); services.AddIdentityServer() .AddJwtBearerClientAuthentication() .AddAspNetIdentity <User>() .AddInMemoryApiResources(IdentityServerConfiguration.GetApis()) .AddInMemoryClients(IdentityServerConfiguration.GetClients()) .AddDeveloperSigningCredential(); var JwtSettingsSection = Configuration.GetSection("JwtSettings"); services.Configure <JwtTokenSettings>(JwtSettingsSection); var JwtTokenSettings = JwtSettingsSection.Get <JwtTokenSettings>(); var key = Encoding.ASCII.GetBytes(JwtTokenSettings.Secret); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer("Bearer", options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() { IssuerSigningKey = new SymmetricSecurityKey(key), ValidIssuer = JwtTokenSettings.Issuer, ValidAudience = JwtTokenSettings.Audience, ClockSkew = TimeSpan.Zero }; options.Events = new JwtBearerEvents { OnMessageReceived = context => { var accessToken = context.Request.Query["access_token"]; // If the request is for hub var path = context.HttpContext.Request.Path; if (!string.IsNullOrEmpty(accessToken) && (path.StartsWithSegments("/ingredients"))) { // Read the token out of the query string context.Token = accessToken; } return(Task.CompletedTask); } }; }); services.AddAuthorization(options => options = PermissionSettings.CreatePermissions(options) ); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "The CRM Api", Version = "v1" }); }); // Register Entity Framework Core services.AddDbContext <ApplicationDbContext>( options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection")) ); // Identity services.AddIdentity <ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); // Identity configuration services.Configure <IdentityOptions>(options => { options.Password.RequireDigit = false; options.Password.RequireLowercase = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequireUppercase = false; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; }); // Register Common Services services.AddHttpContextAccessor(); services.AddTransient <ICurrentUserService, CurrentUserService>(); services.AddTransient <IImageUploadService, ImageUploadPhysicalService>(); services.AddTransient <ITokenCreationService, TokenCreationService>(); // Register Query Services services.AddTransient <ICustomerQueryService, CustomerQueryService>(); services.AddTransient <IUserQueryService, UserQueryService>(); // Register Command handlers services.AddMediatR(Assembly.Load("CRM.Service.EventHandler")); // IdentityServer Configuration services.AddIdentityServer() .AddDeveloperSigningCredential(persistKey: true) .AddInMemoryIdentityResources(IdentityServerConfiguration.GetIdentityResources()) .AddInMemoryApiResources(IdentityServerConfiguration.GetApis()) .AddInMemoryClients(IdentityServerConfiguration.GetClients(Configuration)) .AddAspNetIdentity <ApplicationUser>() .AddProfileService <ProfileService>(); // Schema Authentication services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(options => { options.Authority = Configuration.GetValue <string>("IdentityServer:Authority"); options.RequireHttpsMetadata = false; options.ApiName = "CRM.Api"; options.RoleClaimType = ClaimTypes.Role; }); }