From 7fd9ad111ac2c46dec89dee6a067e3b8b8114e5c Mon Sep 17 00:00:00 2001 From: Anduin Xue Date: Sat, 13 Jul 2024 16:59:52 +0000 Subject: [PATCH 1/4] Update SessionBasedCaptcha.cs to fix #32 Fix: ```log fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1] An unhandled exception has occurred while executing the request. System.ArgumentNullException: Value cannot be null. (Parameter 'source') at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer) at Edi.Captcha.SessionBasedCaptcha.GenerateCaptchaImageBytes(ISession httpSession, Int32 width, Int32 height, String sessionKeyName) at Edi.Captcha.CaptchaImageMiddleware.Invoke(HttpContext context, ISessionBasedCaptcha captcha) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task) fail: MoongladePure.Web.Pages.ErrorModel[0] Error: Value cannot be null. (Parameter 'source'), client IP: 192.168.50.187, request id: 00-ca54f09d4ebac770e57b076fcf3d4390-2782c7aef98f987c-00 fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1] An unhandled exception has occurred while executing the request. System.ArgumentNullException: Value cannot be null. (Parameter 'source') at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer) at Edi.Captcha.SessionBasedCaptcha.GenerateCaptchaImageBytes(ISession httpSession, Int32 width, Int32 height, String sessionKeyName) at Edi.Captcha.CaptchaImageMiddleware.Invoke(HttpContext context, ISessionBasedCaptcha captcha) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task) fail: MoongladePure.Web.Pages.ErrorModel[0] Error: Value cannot be null. (Parameter 'source'), client IP: 192.168.50.187, request id: 00-9c82b9db0ade8b7f1399cd785e3e5f1b-3d39c10d6c9c137b-0 ``` --- src/Edi.Captcha/SessionBasedCaptcha.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Edi.Captcha/SessionBasedCaptcha.cs b/src/Edi.Captcha/SessionBasedCaptcha.cs index 322d386..c2e9b0d 100644 --- a/src/Edi.Captcha/SessionBasedCaptcha.cs +++ b/src/Edi.Captcha/SessionBasedCaptcha.cs @@ -27,6 +27,8 @@ public byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, i EnsureHttpSession(httpSession); var captchaCode = GenerateCaptchaCode(); + + Options.BlockedCodes ??= []; while (Options.BlockedCodes.Contains(captchaCode)) { captchaCode = GenerateCaptchaCode(); @@ -84,4 +86,4 @@ private static void EnsureHttpSession(ISession httpSession) "Session can not be null, please check if Session is enabled in ASP.NET Core via services.AddSession() and app.UseSession()."); } } -} \ No newline at end of file +} From bf600674bcf7e979e7e3a0661f71f06ac93832c3 Mon Sep 17 00:00:00 2001 From: Anduin Xue Date: Sat, 13 Jul 2024 17:00:25 +0000 Subject: [PATCH 2/4] Update Edi.Captcha.csproj to bump version --- src/Edi.Captcha/Edi.Captcha.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Edi.Captcha/Edi.Captcha.csproj b/src/Edi.Captcha/Edi.Captcha.csproj index 34ec148..9148cdb 100644 --- a/src/Edi.Captcha/Edi.Captcha.csproj +++ b/src/Edi.Captcha/Edi.Captcha.csproj @@ -1,6 +1,6 @@ - 3.23.0 + 3.23.1 true edi.wang Edi Wang @@ -22,4 +22,4 @@ - \ No newline at end of file + From 596968d3de542d176634af2445e869838245b1f0 Mon Sep 17 00:00:00 2001 From: Anduin Xue Date: Sat, 13 Jul 2024 17:03:11 +0000 Subject: [PATCH 3/4] Update SessionBasedCaptcha.cs --- src/Edi.Captcha/SessionBasedCaptcha.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Edi.Captcha/SessionBasedCaptcha.cs b/src/Edi.Captcha/SessionBasedCaptcha.cs index c2e9b0d..d71a930 100644 --- a/src/Edi.Captcha/SessionBasedCaptcha.cs +++ b/src/Edi.Captcha/SessionBasedCaptcha.cs @@ -13,7 +13,7 @@ public class SessionBasedCaptchaOptions public FontStyle FontStyle { get; set; } = FontStyle.Regular; public string FontName { get; set; } public bool DrawLines { get; set; } = true; - public string[] BlockedCodes { get; set; } + public string[] BlockedCodes { get; set; } = []; } public abstract class SessionBasedCaptcha : ISessionBasedCaptcha @@ -28,7 +28,6 @@ public byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, i var captchaCode = GenerateCaptchaCode(); - Options.BlockedCodes ??= []; while (Options.BlockedCodes.Contains(captchaCode)) { captchaCode = GenerateCaptchaCode(); From 9a532c56c953dce471df68f77dfe07198c02a346 Mon Sep 17 00:00:00 2001 From: Anduin Xue Date: Sat, 13 Jul 2024 17:03:53 +0000 Subject: [PATCH 4/4] Update SessionBasedCaptcha.cs --- src/Edi.Captcha/SessionBasedCaptcha.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Edi.Captcha/SessionBasedCaptcha.cs b/src/Edi.Captcha/SessionBasedCaptcha.cs index d71a930..3cea5e3 100644 --- a/src/Edi.Captcha/SessionBasedCaptcha.cs +++ b/src/Edi.Captcha/SessionBasedCaptcha.cs @@ -27,7 +27,6 @@ public byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, i EnsureHttpSession(httpSession); var captchaCode = GenerateCaptchaCode(); - while (Options.BlockedCodes.Contains(captchaCode)) { captchaCode = GenerateCaptchaCode();