using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using iPackage.Core.Web.Extensions; using iPackage.Core.Web.Models.Entity; using iPackage.Core.Web.Models.Settings; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; namespace iPackage.Core.Web.WebFramework.Configurations { public static class ServiceConfigExtensions { public static void AddJwtCustomAuthentication(this IServiceCollection serviceCollection, JwtSettings jwtSettings) { serviceCollection.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { var secretKey = Encoding.UTF8.GetBytes(jwtSettings.SecretKey); var validateParammetrs = new TokenValidationParameters { ClockSkew = TimeSpan.Zero, RequireSignedTokens = true, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(secretKey), RequireExpirationTime = true, ValidateLifetime = true, ValidateAudience = true, ValidAudience = jwtSettings.Audience, ValidateIssuer = true, ValidIssuer = jwtSettings.Issuer }; options.RequireHttpsMetadata = true; options.SaveToken = true; options.TokenValidationParameters = validateParammetrs; }); } public static void AddCustomIdentity(this IServiceCollection serviceCollection) where TApplicationContext : DbContext { serviceCollection.AddIdentity(options => { options.Password.RequireLowercase = false; options.Password.RequireUppercase = false; options.Password.RequireDigit = false; options.Password.RequireNonAlphanumeric = false; options.User.RequireUniqueEmail = false; }).AddEntityFrameworkStores().AddDefaultTokenProviders(); } public static void AddCustomApiVersioning(this IServiceCollection serviceCollection) { serviceCollection.AddApiVersioning(options => { options.AssumeDefaultVersionWhenUnspecified = true; options.DefaultApiVersion = new ApiVersion(1, 0); options.ReportApiVersions = true; }); } /// /// In this method we set db context , read github readme for set db context /// /// Type of your DataBase Context /// Type of your asp project startup for check assembly for model builder /// /// public static void AddiPackageDbContext( this IServiceCollection serviceCollection, Action optionAction) where ContextT : DbContext { Action action = options => { options.UseProjectAssembly(typeof(StartUpAssembly).Assembly); }; serviceCollection.AddDbContext(action+optionAction); } } }