ServerCorsPolicy.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using Microsoft.AspNetCore.Cors.Infrastructure;
  3. namespace Jellyfin.Server.Models
  4. {
  5. /// <summary>
  6. /// Server Cors Policy.
  7. /// </summary>
  8. public class ServerCorsPolicy
  9. {
  10. /// <summary>
  11. /// Default policy name.
  12. /// </summary>
  13. public const string DefaultPolicyName = nameof(ServerCorsPolicy);
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="ServerCorsPolicy"/> class.
  16. /// </summary>
  17. /// <param name="corsHosts">The configured cors hosts.</param>
  18. public ServerCorsPolicy(string[] corsHosts)
  19. {
  20. var builder = new CorsPolicyBuilder()
  21. .AllowAnyMethod()
  22. .AllowAnyHeader();
  23. // No hosts configured or only default configured.
  24. if (corsHosts.Length == 0
  25. || (corsHosts.Length == 1
  26. && string.Equals(corsHosts[0], "*", StringComparison.Ordinal)))
  27. {
  28. builder.AllowAnyOrigin();
  29. }
  30. else
  31. {
  32. builder.WithOrigins(corsHosts)
  33. .AllowCredentials();
  34. }
  35. Policy = builder.Build();
  36. }
  37. /// <summary>
  38. /// Gets the cors policy.
  39. /// </summary>
  40. public CorsPolicy Policy { get; }
  41. }
  42. }