InfrastructureServiceRegistration.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. using Microsoft.Extensions.Options;
  23. namespace MTWorkHR.Infrastructure
  24. {
  25. public static class InfrastructureServiceRegistration
  26. {
  27. public static IServiceCollection AddInfrastructureIdentityServices(this IServiceCollection services, AppSettingsConfiguration config)
  28. {
  29. services.AddSingleton(config);
  30. services.AddDbContext<HRDataContext>(options =>
  31. options.UseSqlServer(
  32. config.ConnectionStrings.LocalConnectionString //configuration.GetSection("ConnectionString:MTWorkHRConnectionString").Value
  33. ));
  34. services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<HRDataContext>().AddDefaultTokenProviders();
  35. services.AddAuthentication(options =>
  36. {
  37. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  38. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  39. })
  40. .AddJwtBearer(options =>
  41. {
  42. options.TokenValidationParameters = new TokenValidationParameters
  43. {
  44. ValidateIssuerSigningKey = true,
  45. ValidateIssuer = true,
  46. ValidateAudience = true,
  47. ValidateLifetime = true,
  48. ClockSkew = TimeSpan.Zero,
  49. ValidIssuer = config.JwtSettings.Issuer,
  50. ValidAudience = config.JwtSettings.Audience,
  51. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.JwtSettings.SecretKey))
  52. };
  53. // This is the important part to allow SignalR to work with JWT tokens
  54. options.Events = new JwtBearerEvents
  55. {
  56. OnMessageReceived = context =>
  57. {
  58. var accessToken = context.Request.Query["access_token"];
  59. if (!string.IsNullOrEmpty(accessToken) &&
  60. (context.HttpContext.WebSockets.IsWebSocketRequest || context.Request.Headers["Accept"] == "text/event-stream"))
  61. {
  62. context.Token = accessToken;
  63. }
  64. return Task.CompletedTask;
  65. }
  66.        };
  67.      });
  68. services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
  69. services.AddScoped(typeof(IRepositoryLog<>), typeof(RepositoryLog<>));
  70. services.AddScoped(typeof(ICompanyRepository), typeof(CompanyRepository));
  71. services.AddScoped(typeof(IProjectRepository), typeof(ProjectRepository));
  72. services.AddScoped(typeof(ITaskStatusRepository), typeof(TaskStatusRepository));
  73. services.AddScoped(typeof(IUserTaskRepository), typeof(UserTaskRepository));
  74. services.AddScoped(typeof(IUserTaskAttachmentRepository), typeof(UserTaskAttachmentRepository));
  75. services.AddScoped(typeof(IUserTaskHistoryRepository), typeof(UserTaskHistoryRepository));
  76. services.AddScoped(typeof(ITeamRepository), typeof(TeamRepository));
  77. services.AddScoped(typeof(ITeamUserRepository), typeof(TeamUserRepository));
  78. services.AddScoped(typeof(IMeetingRepository), typeof(MeetingRepository));
  79. services.AddScoped(typeof(IMeetingUserRepository), typeof(MeetingUserRepository));
  80. services.AddScoped(typeof(IAttendanceRepository), typeof(AttendanceRepository));
  81. services.AddScoped(typeof(IOrderAllocationRepository), typeof(OrderAllocationRepository));
  82. services.AddScoped(typeof(IOrderRequestRepository), typeof(OrderRequestRepository));
  83. services.AddScoped(typeof(IOrderTypeRepository), typeof(OrderTypeRepository));
  84. services.AddScoped(typeof(ILeaveTypeRepository), typeof(LeaveTypeRepository));
  85. services.AddScoped(typeof(ICountryLookupRepository), typeof(CountryLookupRepository));
  86. services.AddScoped(typeof(IIndustryRepository), typeof(IndustryRepository));
  87. services.AddScoped(typeof(IJobTitleRepository), typeof(JobTitleRepository));
  88. services.AddScoped(typeof(IQualificationRepository), typeof(QualificationRepository));
  89. services.AddScoped(typeof(IUniversityRepository), typeof(UniversityRepository));
  90. services.AddScoped(typeof(ILoginOTPRepository), typeof(LoginOTPRepository));
  91. services.AddScoped(typeof(ICityRepository), typeof(CityRepository));
  92. services.AddScoped(typeof(IProjectTeamRepository), typeof(ProjectTeamRepository));
  93. services.AddScoped(typeof(IChatMessageRepository), typeof(ChatMessageRepository));
  94. services.AddScoped(typeof(IContractRepository), typeof(ContractRepository));
  95. services.AddScoped(typeof(IContractTaskAttachmentRepository), typeof(ContractTaskAttachmentRepository));
  96. services.AddScoped(typeof(IProjectStageAttachmentRepository), typeof(ProjectStageAttachmentRepository));
  97. services.AddScoped(typeof(IHubConnectionRepository), typeof(HubConnectionRepository));
  98. services.AddScoped(typeof(IPermissionRepository), typeof(PermissionRepository));
  99. services.AddScoped(typeof(IRolePermissionRepository<RolePermission>), typeof(RolePermissionRepository));
  100. services.AddScoped(typeof(IUserRoleRepository<IdentityUserRole<string>>), typeof(UserRoleRepository));
  101. services.AddScoped<IUnitOfWork, UnitOfWork>();
  102. services.AddScoped<IUnitOfWorkLog, UnitOfWorkLog>();
  103. services.AddTransient<IMailSender, MailSender>();
  104. services.AddScoped<ApplicationUserManager>();
  105. services.AddScoped<GlobalInfo>();
  106. //services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
  107. return services;
  108. }
  109. }
  110. }