diff --git a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs index 1d87b097..76a16e7e 100644 --- a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs @@ -2,6 +2,7 @@ using Bootstrap.DataAccess; using Longbow.GiteeAuth; using Longbow.GitHubAuth; +using Longbow.Security.Cryptography; using Longbow.Web; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; @@ -64,6 +65,50 @@ namespace Bootstrap.Admin.Controllers return User.Identity.IsAuthenticated ? (ActionResult)Redirect("~/Home/Index") : View("Login", new LoginModel()); } + /// + /// 短信验证登陆方法 + /// + /// + /// + /// + /// + /// + /// + [HttpPost()] + public async Task Mobile([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, [FromServices]IConfiguration configuration, [FromQuery]string phone, [FromQuery]string code) + { + var option = configuration.GetSection(nameof(SMSOptions)).Get(); + if (UserHelper.AuthenticateMobile(phone, code, option.MD5Key, loginUser => CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser))) + { + var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); + identity.AddClaim(new Claim(ClaimTypes.Name, phone)); + identity.AddClaim(new Claim(ClaimTypes.Role, "Default")); + await HttpContext.SignInAsync(new ClaimsPrincipal(identity)); + + if (UserHelper.RetrieveUserByUserName(identity) == null) + { + var user = new User() + { + ApprovedBy = "Mobile", + ApprovedTime = DateTime.Now, + DisplayName = "手机用户", + UserName = phone, + Password = LgbCryptography.GenerateSalt(), + Icon = "default.jpg", + Description = "手机用户", + App = "2" + }; + UserHelper.Save(user); + CacheCleanUtility.ClearCache(cacheKey: UserHelper.RetrieveUsersDataKey); + } + + // redirect origin url + var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].FirstOrDefault() ?? "~/Home/Index"; + return Redirect(originUrl); + } + return View("Login", new LoginModel() { AuthFailed = true }); + } + /// /// Login the specified userName, password and remember. /// diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs index 0ea0bcfd..b1a64de8 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs @@ -5,15 +5,15 @@ using Longbow.Web; using Longbow.Web.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json.Linq; +using System.Net.Http; +using System.Threading.Tasks; namespace Bootstrap.Admin.Controllers.Api { /// - /// - /// - /// - /// + /// 登陆接口 /// [Route("api/[controller]")] [ApiController] @@ -28,7 +28,7 @@ namespace Bootstrap.Admin.Controllers.Api public QueryData Get([FromQuery]QueryLoginOption value) => value.RetrieveData(); /// - /// + /// JWT 登陆认证接口 /// /// /// @@ -50,7 +50,25 @@ namespace Bootstrap.Admin.Controllers.Api } /// - /// + /// 下发手机短信方法 + /// + /// + /// + /// + /// + [AllowAnonymous] + [HttpPut] + public async Task Put([FromServices]IConfiguration configuration, [FromServices]IHttpClientFactory factory, [FromQuery]string phone) + { + if (string.IsNullOrEmpty(phone)) return false; + + var option = configuration.GetSection(nameof(SMSOptions)).Get(); + option.Phone = phone; + return await factory.CreateClient().SendCode(option); + } + + /// + /// 跨域握手协议 /// /// [AllowAnonymous] diff --git a/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml b/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml index 03a81f35..7f3658ce 100644 --- a/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml +++ b/src/admin/Bootstrap.Admin/Views/Account/Login.cshtml @@ -47,30 +47,58 @@