-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01a9b7e
commit 55d3828
Showing
6 changed files
with
59 additions
and
46 deletions.
There are no files selected for viewing
35 changes: 10 additions & 25 deletions
35
NeoCaptcha.AspnetCore/Attributes/VerifyNeoCaptchaAttribute.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 |
---|---|---|
@@ -1,34 +1,19 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using MyAspNetCoreExtensions.Enitities; | ||
using NeoCaptcha.AspnetCore.Entities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using NeoCaptcha.AspnetCore.Interfaces; | ||
|
||
|
||
namespace NeoCaptcha.AspnetCore.Attributes; | ||
|
||
public class VerifyNeoCaptchaAttribute(ICaptchaGenerator captchaGenerator) : ActionFilterAttribute | ||
public class VerifyNeoCaptchaAttribute : Attribute, IFilterFactory | ||
{ | ||
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
// Find the model that implements RecaptchaCapableModel | ||
|
||
if (context.ActionArguments.Values | ||
.FirstOrDefault(arg => arg is NeoCaptchaCapableModel) is not NeoCaptchaCapableModel model) | ||
{ | ||
context.Result = new BadRequestObjectResult("Invalid model for captcha validation."); | ||
return; | ||
} | ||
public bool IsReusable => false; | ||
|
||
// Perform captcha validation | ||
var validationResult = await captchaGenerator.ValidateCaptcha(model.CaptchaId, model.CaptchaChallenge); | ||
|
||
if (validationResult != CaptchaValidationResult.OK) | ||
{ | ||
context.Result = new BadRequestObjectResult("Captcha validation failed : captcha result :" + validationResult); | ||
return; | ||
} | ||
|
||
// Continue to the action if validation succeeds | ||
await next(); | ||
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) | ||
{ | ||
// Resolve ICaptchaGenerator from the service provider | ||
var captchaGenerator = serviceProvider.GetRequiredService<ICaptchaGenerator>(); | ||
return new VerifyNeoCaptchaFilter(captchaGenerator); | ||
} | ||
} |
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
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,31 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using MyAspNetCoreExtensions.Enitities; | ||
using NeoCaptcha.AspnetCore.Entities; | ||
using NeoCaptcha.AspnetCore.Interfaces; | ||
|
||
namespace NeoCaptcha.AspnetCore; | ||
|
||
public class VerifyNeoCaptchaFilter(ICaptchaGenerator captchaGenerator) : IAsyncActionFilter | ||
{ | ||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
// Locate the model that implements NeoCaptchaCapableModel | ||
if (context.ActionArguments.Values.FirstOrDefault(arg => arg is NeoCaptchaCapableModel) is not NeoCaptchaCapableModel model) | ||
{ | ||
context.Result = new UnauthorizedObjectResult("Invalid captcha model"); | ||
return; | ||
} | ||
|
||
// Validate the captcha | ||
var validationResult = await captchaGenerator.ValidateCaptcha(model.CaptchaId, model.CaptchaChallenge); | ||
|
||
if (validationResult != CaptchaValidationResult.OK) | ||
{ | ||
context.Result = new UnauthorizedObjectResult($"Captcha validation failed: {validationResult}"); | ||
return; | ||
} | ||
|
||
await next(); // Proceed if validation succeeds | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,22 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using NeoCaptcha.AspnetCore.Attributes; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NeoCaptcha.AspnetCore; | ||
using NeoCaptcha.AspnetCore.Interfaces; | ||
|
||
namespace NeoCaptcha.AspnetCore; | ||
|
||
public class VerifyNeoCaptchaFilterFactory(ICaptchaGenerator captchaGenerator) : IFilterFactory | ||
public class VerifyNeoCaptchaFilterFactory : IFilterFactory | ||
{ | ||
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) | ||
private readonly ICaptchaGenerator _captchaGenerator; | ||
|
||
public VerifyNeoCaptchaFilterFactory(ICaptchaGenerator captchaGenerator) | ||
{ | ||
return new VerifyNeoCaptchaAttribute(captchaGenerator); | ||
_captchaGenerator = captchaGenerator; | ||
} | ||
|
||
public bool IsReusable => false; | ||
|
||
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) | ||
{ | ||
// Create the filter instance, passing the required service | ||
return new VerifyNeoCaptchaFilter(_captchaGenerator); | ||
} | ||
} |
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