Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LuAuthExtensions.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using Luticate2.Auth.Attributes.EntityAccessors;
  3. using Luticate2.Auth.Business;
  4. using Luticate2.Auth.DataAccess;
  5. using Luticate2.Auth.Dbo.Basic;
  6. using Luticate2.Auth.Dbo.Users;
  7. using Luticate2.Auth.Interfaces.Groups;
  8. using Luticate2.Auth.Interfaces.Permissions;
  9. using Luticate2.Auth.Interfaces.Tokens;
  10. using Luticate2.Auth.Interfaces.Users;
  11. using Luticate2.Auth.Middlewares;
  12. using Luticate2.Utils.Controllers;
  13. using Luticate2.Utils.Dbo.Basic;
  14. using Microsoft.AspNetCore.Builder;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Microsoft.EntityFrameworkCore;
  18. using Microsoft.Extensions.DependencyInjection;
  19. using Microsoft.Extensions.DependencyInjection.Extensions;
  20. namespace Luticate2.Auth.Controllers
  21. {
  22. public static class LuAuthExtensions
  23. {
  24. public const string RoutePrefix = "luticate";
  25. public const string LuticateItemsLoggedUser = "loggedUser";
  26. public const string TokenCookieName = "luticate2-token";
  27. public static IServiceCollection AddLuticateAuth(this IServiceCollection services,
  28. Action<LuUtilsOptionsDbo> utilsOptionsDelegate, Action<DbContextOptionsBuilder> optionsAction,
  29. Action<LuAuthOptionsDbo> authOptionsDelegate)
  30. {
  31. services.AddLuticateUtils(utilsOptionsDelegate);
  32. services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  33. services.TryAddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
  34. services.TryAddScoped<LuAttrLoggedUserAccessor>();
  35. services.TryAddScoped<LuAttrArgumentAccessor>();
  36. services.TryAddTransient<LuGroupsController>();
  37. services.TryAddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
  38. services.TryAddTransient<LuGroupsDataAccess>();
  39. services.TryAddTransient<LuUsersController>();
  40. services.TryAddTransient<ILuUsersBusiness, LuUsersBusiness>();
  41. services.TryAddTransient<LuUsersDataAccess>();
  42. services.TryAddTransient<ILuTokensBusiness, LuTokensBusiness>();
  43. services.TryAddTransient<LuTokensDataAccess>();
  44. services.TryAddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
  45. services.AddDbContext<LuAuthDatabaseContext>(options =>
  46. {
  47. options.UseInternalServiceProvider(new ServiceCollection()
  48. .AddEntityFrameworkNpgsqlLuticate()
  49. .AddEntityFrameworkNpgsql()
  50. .BuildServiceProvider());
  51. optionsAction.Invoke(options);
  52. }, ServiceLifetime.Transient);
  53. services.Configure(authOptionsDelegate);
  54. return services;
  55. }
  56. public static IMvcBuilder AddLuticateAuth(this IMvcBuilder builder)
  57. {
  58. builder.AddLuticateUtils();
  59. builder.Services.Configure<MvcOptions>(
  60. options =>
  61. {
  62. options.Filters.Add(typeof(LuLoggedUserMiddleware));
  63. options.Filters.Add(typeof(LuPermissionMiddleware));
  64. });
  65. return builder;
  66. }
  67. public static IApplicationBuilder UseLuticateAuth(this IApplicationBuilder app)
  68. {
  69. app.UseLuticateUtils();
  70. return app;
  71. }
  72. public static LuUsersFullDbo GetLuLoggedUser(this HttpContext context)
  73. {
  74. return context.GetLuItems()[LuticateItemsLoggedUser] as LuUsersFullDbo;
  75. }
  76. public static void SetLuLoggedUser(this HttpContext context, LuUsersFullDbo user)
  77. {
  78. context.GetLuItems()[LuticateItemsLoggedUser] = user;
  79. }
  80. public static string GetLuUserToken(this HttpContext context)
  81. {
  82. var token = context.Request.Cookies[TokenCookieName];
  83. return string.IsNullOrWhiteSpace(token) ? null : token;
  84. }
  85. }
  86. }