diff --git a/src/Edi.Captcha.Tests/CaptchaImageMiddlewareTests.cs b/src/Edi.Captcha.Tests/CaptchaImageMiddlewareTests.cs index 460d2df..de4953f 100644 --- a/src/Edi.Captcha.Tests/CaptchaImageMiddlewareTests.cs +++ b/src/Edi.Captcha.Tests/CaptchaImageMiddlewareTests.cs @@ -111,12 +111,12 @@ public async Task Invoke_OK() var middleware = new CaptchaImageMiddleware(app); _captchaMock.Setup(p => - p.GenerateCaptchaImageBytes(It.IsAny(), It.IsAny(), It.IsAny())) + p.GenerateCaptchaImageBytes(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Array.Empty()); await middleware.Invoke(httpContextMock.Object, _captchaMock.Object); - _captchaMock.Verify(p => p.GenerateCaptchaImageBytes(It.IsAny(), It.IsAny(), It.IsAny())); + _captchaMock.Verify(p => p.GenerateCaptchaImageBytes(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); } } } diff --git a/src/Edi.Captcha/ISessionBasedCaptcha.cs b/src/Edi.Captcha/ISessionBasedCaptcha.cs index 23060f2..86b3088 100644 --- a/src/Edi.Captcha/ISessionBasedCaptcha.cs +++ b/src/Edi.Captcha/ISessionBasedCaptcha.cs @@ -5,10 +5,10 @@ namespace Edi.Captcha { public interface ISessionBasedCaptcha { - byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, int height = 36); + byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, int height = 36, string sessionKeyName = null); - FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int width = 100, int height = 36); + FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int width = 100, int height = 36, string sessionKeyName = null); - bool Validate(string userInputCaptcha, ISession httpSession, bool ignoreCase = true, bool dropSession = true); + bool Validate(string userInputCaptcha, ISession httpSession, bool ignoreCase = true, bool dropSession = true, string sessionKeyName = null); } } \ No newline at end of file diff --git a/src/Edi.Captcha/SessionBasedCaptcha.cs b/src/Edi.Captcha/SessionBasedCaptcha.cs index 63fa23b..408cb1a 100644 --- a/src/Edi.Captcha/SessionBasedCaptcha.cs +++ b/src/Edi.Captcha/SessionBasedCaptcha.cs @@ -24,23 +24,23 @@ public abstract class SessionBasedCaptcha : ISessionBasedCaptcha public abstract string GenerateCaptchaCode(); - public byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, int height = 36) + public byte[] GenerateCaptchaImageBytes(ISession httpSession, int width = 100, int height = 36, string sessionKeyName = null) { EnsureHttpSession(httpSession); var captchaCode = GenerateCaptchaCode(); var result = CaptchaImageGenerator.GetImage(width, height, captchaCode, Options.FontName, Options.FontStyle); - httpSession.SetString(Options.SessionName, result.CaptchaCode); + httpSession.SetString(sessionKeyName ?? Options.SessionName, result.CaptchaCode); return result.CaptchaByteData; } - public FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int width = 100, int height = 36) + public FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int width = 100, int height = 36, string sessionKeyName = null) { EnsureHttpSession(httpSession); var captchaCode = GenerateCaptchaCode(); var result = CaptchaImageGenerator.GetImage(width, height, captchaCode, Options.FontName, Options.FontStyle); - httpSession.SetString(Options.SessionName, result.CaptchaCode); + httpSession.SetString(sessionKeyName ?? Options.SessionName, result.CaptchaCode); Stream s = new MemoryStream(result.CaptchaByteData); return new FileStreamResult(s, "image/png"); } @@ -53,17 +53,17 @@ public FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int /// Ignore Case (default = true) /// Whether to drop session regardless of the validation pass or not (default = true) /// Is Valid Captcha Challenge - public bool Validate(string userInputCaptcha, ISession httpSession, bool ignoreCase = true, bool dropSession = true) + public bool Validate(string userInputCaptcha, ISession httpSession, bool ignoreCase = true, bool dropSession = true, string sessionKeyName = null) { if (string.IsNullOrWhiteSpace(userInputCaptcha)) { return false; } - var codeInSession = httpSession.GetString(Options.SessionName); + var codeInSession = httpSession.GetString(sessionKeyName ?? Options.SessionName); var isValid = string.Compare(userInputCaptcha, codeInSession, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); if (dropSession) { - httpSession.Remove(Options.SessionName); + httpSession.Remove(sessionKeyName ?? Options.SessionName); } return isValid == 0; }