-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHCaptchaExtensions.cs
44 lines (37 loc) · 1.79 KB
/
HCaptchaExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright © Benjamin Abt 2020-2024, all rights reserved
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Refit;
namespace BenjaminAbt.HCaptcha.AspNetCore;
/// <summary>
/// Provides extension methods for registering hCaptcha services in the dependency injection container.
/// </summary>
public static class HCaptchaExtensions
{
/// <summary>
/// Adds hCaptcha services to the <see cref="IServiceCollection"/> using the provided configuration section.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to which the hCaptcha services will be added.</param>
/// <param name="section">The <see cref="IConfigurationSection"/> containing the hCaptcha configuration settings.</param>
/// <returns>The <see cref="IServiceCollection"/> with hCaptcha services added.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="services"/> or <paramref name="section"/> is <c>null</c>.
/// </exception>
public static IServiceCollection AddHCaptcha(this IServiceCollection services, IConfigurationSection section)
{
// Bind configuration to HCaptchaOptions
HCaptchaOptions captchaOptions = new();
section.Bind(captchaOptions);
// Configure options to be injected wherever needed
services.Configure<HCaptchaOptions>(section);
// Register the Refit client for IHCaptchaApi with the base URL from configuration
services.AddRefitClient<IHCaptchaApi>()
.ConfigureHttpClient(c =>
{
c.BaseAddress = new Uri(captchaOptions.ApiBaseUrl);
});
// Register the hCaptcha provider
services.AddScoped<IHCaptchaProvider, HCaptchaProvider>();
return services;
}
}