Skip to content

Commit

Permalink
fix:liukuo362573#157 ubantu 系统无法登陆,验证码图片无法正常展示,使用组件 lazyCapture and u…
Browse files Browse the repository at this point in the history
…pgrade cache Microsoft.Extensions.Caching.Memory to version8.0.0
  • Loading branch information
bingtianyiyan committed Jun 28, 2024
1 parent 6ec7256 commit e24ca30
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion YiSha.Cache/YiSha.MemoryCache/YiSha.MemoryCache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions YiSha.Util/YiSha.Util/YiSha.Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
Expand Down
21 changes: 16 additions & 5 deletions YiSha.Web/YiSha.Admin.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using YiSha.Entity.OrganizationManage;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Lazy.Captcha.Core;
using System.IO;

namespace YiSha.Admin.Web.Controllers
{
Expand All @@ -26,6 +28,12 @@ public class HomeController : BaseController
private UserBLL userBLL = new UserBLL();
private LogLoginBLL logLoginBLL = new LogLoginBLL();
private MenuAuthorizeBLL menuAuthorizeBLL = new MenuAuthorizeBLL();
private readonly ICaptcha _captcha;

public HomeController(ICaptcha captcha)
{
_captcha = captcha;
}

#region 视图功能
[HttpGet]
Expand Down Expand Up @@ -129,11 +137,14 @@ public IActionResult Skin()
public IActionResult GetCaptchaImage()
{
string sessionId = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>().HttpContext.Session.Id;

Tuple<string, int> captchaCode = CaptchaHelper.GetCaptchaCode();
byte[] bytes = CaptchaHelper.CreateCaptchaImage(captchaCode.Item1);
new SessionHelper().WriteSession("CaptchaCode", captchaCode.Item2);
return File(bytes, @"image/jpeg");
//Tuple<string, int> captchaCode = CaptchaHelper.GetCaptchaCode();
//byte[] bytes = CaptchaHelper.CreateCaptchaImage(captchaCode.Item1);
//new SessionHelper().WriteSession("CaptchaCode", captchaCode.Item2);
//return File(bytes, @"image/jpeg");
var info = _captcha.Generate(sessionId);
var stream = new MemoryStream(info.Bytes);
new SessionHelper().WriteSession("CaptchaCode", Convert.ToInt32(info.Code));
return File(stream, "image/jpeg");
}
#endregion

Expand Down
21 changes: 21 additions & 0 deletions YiSha.Web/YiSha.Admin.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using YiSha.Util.Model;
using YiSha.Admin.Web.Controllers;
using Microsoft.Extensions.Logging;
using Lazy.Captcha.Core.Generator;
using Lazy.Captcha.Core;

namespace YiSha.Admin.Web
{
Expand Down Expand Up @@ -68,6 +70,25 @@ public void ConfigureServices(IServiceCollection services)

services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + "DataProtection"));

//capture code
services.AddCaptcha(Configuration, option =>
{
option.CaptchaType = CaptchaType.ARITHMETIC; // 验证码类型
option.CodeLength = 2; // 验证码长度, 要放在CaptchaType设置后. 当类型为算术表达式时,长度代表操作的个数
option.ExpirySeconds = 60; // 验证码过期时间
option.IgnoreCase = true; // 比较时是否忽略大小写
option.StoreageKeyPrefix = ""; // 存储键前缀
option.ImageOption.FrameDelay = 30; // 每帧延迟,Animation=true时有效, 默认30
option.ImageOption.Width = 150; // 验证码宽度
option.ImageOption.Height = 50; // 验证码高度
//option.ImageOption.BackgroundColor = SixLabors.ImageSharp.Color.White; // 验证码背景色
option.ImageOption.InterferenceLineCount = 2; // 干扰线数量
option.ImageOption.FontSize = 36; // 字体大小
option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; // 字体
option.ImageOption.TextBold = true;// 粗体,该配置2.0.3新增
});

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // 注册Encoding

GlobalContext.SystemConfig = Configuration.GetSection("SystemConfig").Get<SystemConfig>();
Expand Down

0 comments on commit e24ca30

Please sign in to comment.