InfrastructureServiceRegistration.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.IdentityModel.Tokens;
  7. using MTWorkHR.Core.Email;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.IRepositories.Base;
  10. using MTWorkHR.Core.IRepositories;
  11. using MTWorkHR.Core.UnitOfWork;
  12. using MTWorkHR.Infrastructure.DBContext;
  13. using MTWorkHR.Infrastructure.EmailService;
  14. using MTWorkHR.Infrastructure.Repositories;
  15. using MTWorkHR.Infrastructure.UnitOfWorks;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using MTWorkHR.Infrastructure.Entities;
  22. namespace MTWorkHR.Infrastructure
  23. {
  24. public static class InfrastructureServiceRegistration
  25. {
  26. public static IServiceCollection AddInfrastructureIdentityServices(this IServiceCollection services, AppSettingsConfiguration config)
  27. {
  28. services.AddSingleton(config);
  29. var vv = config.ConnectionStrings.MTWorkHRConnectionString;
  30. services.AddDbContext<HRDataContext>(options =>
  31. options.UseSqlServer(
  32. config.ConnectionStrings.MTWorkHRConnectionString //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
  33. ));
  34. services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<HRDataContext>().AddDefaultTokenProviders();
  35. services.AddAuthentication(options => {
  36. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // "bearer"
  37. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  38. }).AddJwtBearer(o=> o.TokenValidationParameters = new TokenValidationParameters
  39. {
  40. ValidateIssuerSigningKey = true,
  41. ValidateIssuer = true,
  42. ValidateAudience = true,
  43. ValidateLifetime = true,
  44. ClockSkew = TimeSpan.Zero,
  45. ValidIssuer = config.JwtSettings.Issuer,
  46. ValidAudience = config.JwtSettings.Audience,
  47. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.JwtSettings.SecretKey))
  48. }) ;
  49. services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
  50. services.AddScoped(typeof(IRepositoryLog<>), typeof(RepositoryLog<>));
  51. services.AddScoped(typeof(ICompanyRepository), typeof(CompanyRepository));
  52. services.AddScoped(typeof(IProjectRepository), typeof(ProjectRepository));
  53. services.AddScoped(typeof(ITaskStatusRepository), typeof(TaskStatusRepository));
  54. services.AddScoped(typeof(IUserTaskRepository), typeof(UserTaskRepository));
  55. services.AddScoped(typeof(IUserTaskAttachmentRepository), typeof(UserTaskAttachmentRepository));
  56. services.AddScoped(typeof(IUserTaskHistoryRepository), typeof(UserTaskHistoryRepository));
  57. services.AddScoped(typeof(ITeamRepository), typeof(TeamRepository));
  58. services.AddScoped(typeof(IMeetingRepository), typeof(MeetingRepository));
  59. services.AddScoped(typeof(IPermissionRepository), typeof(PermissionRepository));
  60. services.AddScoped(typeof(IRolePermissionRepository<RolePermission>), typeof(RolePermissionRepository));
  61. services.AddScoped(typeof(IUserRoleRepository<IdentityUserRole<string>>), typeof(UserRoleRepository));
  62. services.AddScoped<IUnitOfWork, UnitOfWork>();
  63. services.AddScoped<IUnitOfWorkLog, UnitOfWorkLog>();
  64. services.AddTransient<IMailSender, MailSender>();
  65. services.AddScoped<ApplicationUserManager>();
  66. services.AddScoped<GlobalInfo>();
  67. services.AddScoped<IEmployeeRepository, EmployeeRepository>();
  68. //services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
  69. return services;
  70. }
  71. }
  72. }