Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Startup.cs 2.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Microsoft.AspNetCore.Authentication.Cookies;
  2. using Microsoft.AspNetCore.Authentication.OpenIdConnect;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. namespace Sample.RabbitMQ.Postgres.DashboardAuth
  7. {
  8. public class Startup
  9. {
  10. private readonly IConfiguration _configuration;
  11. public Startup(IConfiguration configuration)
  12. {
  13. _configuration = configuration;
  14. }
  15. public void ConfigureServices(IServiceCollection services)
  16. {
  17. services.AddHttpContextAccessor();
  18. services
  19. .AddAuthorization()
  20. .AddAuthentication(options =>
  21. {
  22. options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  23. options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
  24. })
  25. .AddCookie()
  26. .AddOpenIdConnect(options =>
  27. {
  28. options.Authority = "https://demo.identityserver.io/";
  29. options.ClientId = "interactive.confidential";
  30. options.ClientSecret = "secret";
  31. options.ResponseType = "code";
  32. options.UsePkce = true;
  33. options.Scope.Clear();
  34. options.Scope.Add("openid");
  35. options.Scope.Add("profile");
  36. });
  37. services.AddCap(cap =>
  38. {
  39. cap.UsePostgreSql(p =>
  40. {
  41. p.ConnectionString = _configuration.GetConnectionString("Postgres");
  42. });
  43. /*
  44. * Use the command below to start a rabbitmq instance locally:
  45. * docker run -d --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:management
  46. */
  47. cap.UseRabbitMQ(r =>
  48. {
  49. r.Port = 5672;
  50. r.HostName = "127.0.0.1";
  51. r.UserName = "guest";
  52. r.Password = "guest";
  53. });
  54. cap.UseDashboard(d =>
  55. {
  56. d.UseChallengeOnAuth = true;
  57. d.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
  58. d.Authorization = new[] {new HttpContextDashboardFilter()};
  59. });
  60. });
  61. services.AddControllers();
  62. }
  63. public void Configure(IApplicationBuilder app)
  64. {
  65. app.UseAuthentication();
  66. app.UseRouting();
  67. app.UseAuthorization();
  68. app.UseEndpoints(endpoints =>
  69. {
  70. endpoints.MapControllers();
  71. });
  72. }
  73. }
  74. }