You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
2.7 KiB

  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.Authorization = new[] {new HttpContextDashboardFilter()};
  58. });
  59. });
  60. services.AddControllers();
  61. }
  62. public void Configure(IApplicationBuilder app)
  63. {
  64. app.UseAuthentication();
  65. app.UseRouting();
  66. app.UseAuthorization();
  67. app.UseEndpoints(endpoints =>
  68. {
  69. endpoints.MapControllers();
  70. });
  71. }
  72. }
  73. }