InfrastructureServiceRegistration.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. services.AddDbContext<HRDataContext>(options =>
  30. options.UseSqlServer(
  31. config.ConnectionStrings.LocalConnectionString //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
  32. ));
  33. services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<HRDataContext>().AddDefaultTokenProviders();
  34. services.AddAuthentication(options => {
  35. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // "bearer"
  36. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  37. }).AddJwtBearer(o=> o.TokenValidationParameters = new TokenValidationParameters
  38. {
  39. ValidateIssuerSigningKey = true,
  40. ValidateIssuer = true,
  41. ValidateAudience = true,
  42. ValidateLifetime = true,
  43. ClockSkew = TimeSpan.Zero,
  44. ValidIssuer = config.JwtSettings.Issuer,
  45. ValidAudience = config.JwtSettings.Audience,
  46. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.JwtSettings.SecretKey))
  47. }) ;
  48. services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
  49. services.AddScoped(typeof(IRepositoryLog<>), typeof(RepositoryLog<>));
  50. services.AddScoped(typeof(ICompanyRepository), typeof(CompanyRepository));
  51. services.AddScoped(typeof(IProjectRepository), typeof(ProjectRepository));
  52. services.AddScoped(typeof(ITaskStatusRepository), typeof(TaskStatusRepository));
  53. services.AddScoped(typeof(IUserTaskRepository), typeof(UserTaskRepository));
  54. services.AddScoped(typeof(IUserTaskAttachmentRepository), typeof(UserTaskAttachmentRepository));
  55. services.AddScoped(typeof(IUserTaskHistoryRepository), typeof(UserTaskHistoryRepository));
  56. services.AddScoped(typeof(ITeamRepository), typeof(TeamRepository));
  57. services.AddScoped(typeof(ITeamUserRepository), typeof(TeamUserRepository));
  58. services.AddScoped(typeof(IMeetingRepository), typeof(MeetingRepository));
  59. services.AddScoped(typeof(IMeetingUserRepository), typeof(MeetingUserRepository));
  60. services.AddScoped(typeof(IAttendanceRepository), typeof(AttendanceRepository));
  61. services.AddScoped(typeof(IOrderAllocationRepository), typeof(OrderAllocationRepository));
  62. services.AddScoped(typeof(IOrderRequestRepository), typeof(OrderRequestRepository));
  63. services.AddScoped(typeof(IOrderTypeRepository), typeof(OrderTypeRepository));
  64. services.AddScoped(typeof(ILeaveTypeRepository), typeof(LeaveTypeRepository));
  65. services.AddScoped(typeof(ICountryLookupRepository), typeof(CountryLookupRepository));
  66. services.AddScoped(typeof(IIndustryRepository), typeof(IndustryRepository));
  67. services.AddScoped(typeof(IJobTitleRepository), typeof(JobTitleRepository));
  68. services.AddScoped(typeof(IQualificationRepository), typeof(QualificationRepository));
  69. services.AddScoped(typeof(IUniversityRepository), typeof(UniversityRepository));
  70. services.AddScoped(typeof(ILoginOTPRepository), typeof(LoginOTPRepository));
  71. services.AddScoped(typeof(ICityRepository), typeof(CityRepository));
  72. services.AddScoped(typeof(IProjectTeamRepository), typeof(ProjectTeamRepository));
  73. services.AddScoped(typeof(IPermissionRepository), typeof(PermissionRepository));
  74. services.AddScoped(typeof(IRolePermissionRepository<RolePermission>), typeof(RolePermissionRepository));
  75. services.AddScoped(typeof(IUserRoleRepository<IdentityUserRole<string>>), typeof(UserRoleRepository));
  76. services.AddScoped<IUnitOfWork, UnitOfWork>();
  77. services.AddScoped<IUnitOfWorkLog, UnitOfWorkLog>();
  78. services.AddTransient<IMailSender, MailSender>();
  79. services.AddScoped<ApplicationUserManager>();
  80. services.AddScoped<GlobalInfo>();
  81. services.AddScoped<IEmployeeRepository, EmployeeRepository>();
  82. //services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
  83. return services;
  84. }
  85. }
  86. }