Skip to content

Commit

Permalink
Merge pull request #8 from ManualMaster/master
Browse files Browse the repository at this point in the history
Added custom Session key name setting
  • Loading branch information
EdiWang authored Jun 12, 2022
2 parents 9ccc9cb + cc5b9af commit dc9d590
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Edi.Captcha.Tests/CaptchaImageMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public async Task Invoke_OK()
var middleware = new CaptchaImageMiddleware(app);

_captchaMock.Setup(p =>
p.GenerateCaptchaImageBytes(It.IsAny<ISession>(), It.IsAny<int>(), It.IsAny<int>()))
p.GenerateCaptchaImageBytes(It.IsAny<ISession>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>()))
.Returns(Array.Empty<byte>());

await middleware.Invoke(httpContextMock.Object, _captchaMock.Object);

_captchaMock.Verify(p => p.GenerateCaptchaImageBytes(It.IsAny<ISession>(), It.IsAny<int>(), It.IsAny<int>()));
_captchaMock.Verify(p => p.GenerateCaptchaImageBytes(It.IsAny<ISession>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>()));
}
}
}
6 changes: 3 additions & 3 deletions src/Edi.Captcha/ISessionBasedCaptcha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 7 additions & 7 deletions src/Edi.Captcha/SessionBasedCaptcha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -53,17 +53,17 @@ public FileStreamResult GenerateCaptchaImageFileStream(ISession httpSession, int
/// <param name="ignoreCase">Ignore Case (default = true)</param>
/// <param name="dropSession">Whether to drop session regardless of the validation pass or not (default = true)</param>
/// <returns>Is Valid Captcha Challenge</returns>
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;
}
Expand Down

0 comments on commit dc9d590

Please sign in to comment.