iPackage/iPackage.Core.Web/WebFramework/Configurations/ServiceConfigExtensions.cs

93 lines
3.8 KiB
C#

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<TApplicationContext>(this IServiceCollection serviceCollection) where TApplicationContext : DbContext
{
serviceCollection.AddIdentity<Admin, Role>(options =>
{
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireDigit = false;
options.Password.RequireNonAlphanumeric = false;
options.User.RequireUniqueEmail = false;
}).AddEntityFrameworkStores<TApplicationContext>().AddDefaultTokenProviders();
}
public static void AddCustomApiVersioning(this IServiceCollection serviceCollection)
{
serviceCollection.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ReportApiVersions = true;
});
}
/// <summary>
/// In this method we set db context , read github readme for set db context
/// </summary>
/// <typeparam name="ContextT">Type of your DataBase Context</typeparam>
/// <typeparam name="StartUpAssembly">Type of your asp project startup for check assembly for model builder</typeparam>
/// <param name="serviceCollection"></param>
/// <param name="optionAction"></param>
public static void AddiPackageDbContext<ContextT,StartUpAssembly>(
this IServiceCollection serviceCollection,
Action<DbContextOptionsBuilder> optionAction)
where ContextT : DbContext
{
Action<DbContextOptionsBuilder> action = options =>
{
options.UseProjectAssembly(typeof(StartUpAssembly).Assembly);
};
serviceCollection.AddDbContext<ContextT>(action+optionAction);
}
}
}