-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from cswebworks/master
Added plain ValidationAttribute for model-based validation
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
reCAPTCHA.AspNetCore.Example/Controllers/SomeAPIControlelr.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using reCAPTCHA.AspNetCore.Attributes; | ||
using reCAPTCHA.AspNetCore.Example.Models; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace reCAPTCHA.AspNetCore.Example.Controllers | ||
{ | ||
[ApiController] | ||
public class SomeApiController : Controller | ||
{ | ||
[HttpGet] | ||
public ActionResult SomeAction(string someValue, [Required, RecaptchaResponse] string reCaptchaResponseCode) | ||
{ | ||
return Ok($"I have done something with {someValue} after reCaptcha was validated"); | ||
} | ||
|
||
[HttpPost] | ||
public ActionResult SomeAction(OtherModel model) | ||
{ | ||
return Ok($"I have done something after reCaptcha was validated"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using reCAPTCHA.AspNetCore.Attributes; | ||
|
||
namespace reCAPTCHA.AspNetCore.Example.Models | ||
{ | ||
public class OtherModel | ||
{ | ||
[RecaptchaResponse(MinimumScore = 0.5)] | ||
public string ReCaptchaResponseCode { get; set; } | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
reCAPTCHA.AspNetCore/Attributes/RecaptchaResponseAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace reCAPTCHA.AspNetCore.Attributes | ||
{ | ||
/// <summary> | ||
/// Validates reCaptcha response code bound to a property, parameter or field. | ||
/// </summary> | ||
/// <seealso cref="ValidationAttribute" /> | ||
/// <seealso cref="IRecaptchaService.Validate(string)"/> | ||
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field)] | ||
public class RecaptchaResponseAttribute : ValidationAttribute | ||
{ | ||
/// <summary> | ||
/// Gets a value that indicates whether the attribute requires validation context. | ||
/// </summary> | ||
public override bool RequiresValidationContext => true; | ||
|
||
/// <summary> | ||
/// Gets or sets the minimum score. | ||
/// </summary> | ||
/// <value> | ||
/// The minimum score. | ||
/// </value> | ||
/// <seealso cref="ValidateRecaptchaAttribute._minimumScore"/> | ||
public double MinimumScore { get; set; } | ||
|
||
/// <summary> | ||
/// Returns wether the captcha validation was deemed successful based on the value of a member to attribute has been applied to. | ||
/// </summary> | ||
/// <param name="value">The value to validate.</param> | ||
/// <param name="validationContext">The context information about the validation operation.</param> | ||
/// <returns> | ||
/// An instance of the <see cref="System.ComponentModel.DataAnnotations.ValidationResult" /> class. | ||
/// </returns> | ||
/// <exception cref="ConfigurationErrorsException">When reCaptcha has not been configured.</exception> | ||
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
{ | ||
if (!(value is string responseCode) | ||
|| string.IsNullOrWhiteSpace(responseCode)) | ||
return null; | ||
|
||
var validationservice = validationContext.GetService<IRecaptchaService>(); | ||
|
||
if(validationservice is null) | ||
throw new ConfigurationErrorsException($"{typeof(IRecaptchaService).Assembly.GetName().Name} has not been configured"); | ||
|
||
var response = validationservice.Validate(responseCode).Result; | ||
|
||
if (response.success | ||
&& (response.score == 0 || response.score >= MinimumScore)) | ||
|
||
return ValidationResult.Success; | ||
|
||
else | ||
|
||
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters