尝试为 .NET Core 6 实现 DNTCaptcha
Trying to implement DNTCaptcha for .NET Core 6
所以我知道如何在以前版本的 .NET Core 中实现它。但是我在 .NET Core 6 上遇到了麻烦。
在.Net 3.1或5中,从documentation开始,是这样进行的:
- 安装 nuget 包:“安装包 DNTCaptcha.Core”
- 在 _ViewImports.cshtml 文件中添加
@addTagHelper *, DNTCaptcha.Core
- 以
<dnt-captcha asp-captcha-generator-max="9000" //code// />
的形式使用帮助标签
- 在启动 class 中注册默认提供程序
services.AddDNTCaptcha();
。
- 包括 jQuery、字体很棒和 jquery.unobtrusive-ajax
- 并且在控制器中,
ValidateDNTCaptcha
属性到您的操作方法
这很好用。但是对于 .NET Core 6,没有 Startup class。那么我该如何注册默认提供者呢?我在程序 class:
中尝试这样做
builder.Services.AddDNTCaptcha(options =>
options.UseCookieStorageProvider()
.ShowThousandsSeparators(false)
);
但这给了我错误:
InvalidOperationException: Please set the `options.WithEncryptionKey(...)`.
我尝试查看其他资源和文档,但它们都使用了 Startup class。那么如何在 .NET Core 6 中实现它呢?
这样做:
IWebHostEnvironment _env = builder.Environment;
builder.services.AddDNTCaptcha(options =>
{
// options.UseSessionStorageProvider() // -> It doesn't rely on the server or client's times. Also it's the safest one.
// options.UseMemoryCacheStorageProvider() // -> It relies on the server's times. It's safer than the CookieStorageProvider.
options.UseCookieStorageProvider(SameSiteMode.Strict /* If you are using CORS, set it to `None` */) // -> It relies on the server and client's times. It's ideal for scalability, because it doesn't save anything in the server's memory.
// .UseDistributedCacheStorageProvider() // --> It's ideal for scalability using `services.AddStackExchangeRedisCache()` for instance.
// .UseDistributedSerializationProvider()
// Don't set this line (remove it) to use the installed system's fonts (FontName = "Tahoma").
// Or if you want to use a custom font, make sure that font is present in the wwwroot/fonts folder and also use a good and complete font!
.UseCustomFont(Path.Combine(_env.WebRootPath, "fonts", "IRANSans(FaNum)_Bold.ttf"))
.AbsoluteExpiration(minutes: 7)
.ShowThousandsSeparators(false)
.WithNoise(pixelsDensity: 25, linesCount: 3)
.WithEncryptionKey("This is my secure key!")
.InputNames(// This is optional. Change it if you don't like the default names.
new DNTCaptchaComponent
{
CaptchaHiddenInputName = "DNT_CaptchaText",
CaptchaHiddenTokenName = "DNT_CaptchaToken",
CaptchaInputName = "DNT_CaptchaInputText"
})
.Identifier("dnt_Captcha")// This is optional. Change it if you don't like its default name.
;
});
所以我知道如何在以前版本的 .NET Core 中实现它。但是我在 .NET Core 6 上遇到了麻烦。
在.Net 3.1或5中,从documentation开始,是这样进行的:
- 安装 nuget 包:“安装包 DNTCaptcha.Core”
- 在 _ViewImports.cshtml 文件中添加
@addTagHelper *, DNTCaptcha.Core
- 以
<dnt-captcha asp-captcha-generator-max="9000" //code// />
的形式使用帮助标签
- 在启动 class 中注册默认提供程序
services.AddDNTCaptcha();
。 - 包括 jQuery、字体很棒和 jquery.unobtrusive-ajax
- 并且在控制器中,
ValidateDNTCaptcha
属性到您的操作方法
这很好用。但是对于 .NET Core 6,没有 Startup class。那么我该如何注册默认提供者呢?我在程序 class:
中尝试这样做builder.Services.AddDNTCaptcha(options =>
options.UseCookieStorageProvider()
.ShowThousandsSeparators(false)
);
但这给了我错误:
InvalidOperationException: Please set the `options.WithEncryptionKey(...)`.
我尝试查看其他资源和文档,但它们都使用了 Startup class。那么如何在 .NET Core 6 中实现它呢?
这样做:
IWebHostEnvironment _env = builder.Environment;
builder.services.AddDNTCaptcha(options =>
{
// options.UseSessionStorageProvider() // -> It doesn't rely on the server or client's times. Also it's the safest one.
// options.UseMemoryCacheStorageProvider() // -> It relies on the server's times. It's safer than the CookieStorageProvider.
options.UseCookieStorageProvider(SameSiteMode.Strict /* If you are using CORS, set it to `None` */) // -> It relies on the server and client's times. It's ideal for scalability, because it doesn't save anything in the server's memory.
// .UseDistributedCacheStorageProvider() // --> It's ideal for scalability using `services.AddStackExchangeRedisCache()` for instance.
// .UseDistributedSerializationProvider()
// Don't set this line (remove it) to use the installed system's fonts (FontName = "Tahoma").
// Or if you want to use a custom font, make sure that font is present in the wwwroot/fonts folder and also use a good and complete font!
.UseCustomFont(Path.Combine(_env.WebRootPath, "fonts", "IRANSans(FaNum)_Bold.ttf"))
.AbsoluteExpiration(minutes: 7)
.ShowThousandsSeparators(false)
.WithNoise(pixelsDensity: 25, linesCount: 3)
.WithEncryptionKey("This is my secure key!")
.InputNames(// This is optional. Change it if you don't like the default names.
new DNTCaptchaComponent
{
CaptchaHiddenInputName = "DNT_CaptchaText",
CaptchaHiddenTokenName = "DNT_CaptchaToken",
CaptchaInputName = "DNT_CaptchaInputText"
})
.Identifier("dnt_Captcha")// This is optional. Change it if you don't like its default name.
;
});