【问题标题】:How to implement captcha with in ASP.NET Core 5.0 app with IdentityServer?如何在带有 IdentityServer 的 ASP.NET Core 5.0 应用程序中实现验证码?
【发布时间】:2021-10-29 11:34:02
【问题描述】:

在具有 Identity 和 IdentityServer4 的 ASP.NET Core 中 - 我需要为新用户添加保护措施。

您如何实施验证码/验证码?

【问题讨论】:

    标签: asp.net-core identityserver4 captcha


    【解决方案1】:

    我找到了这个简单的代码:https://github.com/Jarda29/GoogleReCaptcha.V3

    1. 添加 nuget(我实际上使用了源代码 - 它实际上只有一个文件)
    2. 来自 IdentityServer 的脚手架 Register
    3. 使用 Recaptcha 注册您的网站(您需要单独的 prod 和 localhost 密钥)
    4. 修改Register.cshtml:
    <div class="row">
        <div class="col-md-4">
            <section>
                <form asp-route-returnUrl="@Model.ReturnUrl" method="post">
                    <h4>Create a new account.</h4>
                    <hr />
                    <div asp-validation-summary="All" class="text-danger"></div>
                    <div class="form-group">
                        <label asp-for="Input.Email"></label>
                        <input asp-for="Input.Email" class="form-control" />
                        <span asp-validation-for="Input.Email" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="Input.Password"></label>
                        <input asp-for="Input.Password" class="form-control" />
                        <span asp-validation-for="Input.Password" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="Input.ConfirmPassword"></label>
                        <input asp-for="Input.ConfirmPassword" class="form-control" />
                        <span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
                    </div>
                    <button type="submit" class="btn btn-primary">Register</button>
                    <input type="hidden" name="captcha" id="captchaInput" value="" />
                </form>
            </section>
        </div>
    </div>
    
    @section Scripts {
        <script src="https://www.google.com/recaptcha/api.js?render=@Configuration["Recaptcha:siteKey"]"></script>
        <script>
            grecaptcha.ready(function() {
                grecaptcha.execute('@Configuration["Recaptcha:siteKey"]', { action: 'contact' }).then(function (token) {
                    $("#captchaInput").val(token);
                });
            });
        </script>
    
        <partial name="_ValidationScriptsPartial" />
    }
    
    1. 修改Register.cshtml.cs
                if (ModelState.IsValid)
                {
                    if (!await _captchaValidator.IsCaptchaPassedAsync(captcha))
                    {
                        ModelState.AddModelError("captcha", "Captcha validation failed");
                    }
                    else
                    {
                        var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email };
                        var result = await _userManager.CreateAsync(user, Input.Password);
                        if (result.Succeeded)
                        {
                            _logger.LogInformation("User created a new account with password.");
    
    
    1. 添加到 Startup.cs:services.AddHttpClient&lt;ICaptchaValidator, RecaptchaValidator&gt;();

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2021-09-30
      • 2018-10-25
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多