From 7a75f9ec688e11126e5b1ff78f9c3ada75ab9588 Mon Sep 17 00:00:00 2001 From: Argo Date: Mon, 16 Sep 2019 17:59:23 +0800 Subject: [PATCH] =?UTF-8?q?!31=20=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0=E6=89=8B=E6=9C=BA=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AccountController.cs | 45 +++++ .../Controllers/Api/LoginController.cs | 30 +++- .../Views/Account/Login.cshtml | 44 ++++- .../appsettings.Development.json | 4 + src/admin/Bootstrap.Admin/appsettings.json | 6 +- .../Bootstrap.Admin/wwwroot/css/login.css | 11 +- src/admin/Bootstrap.Admin/wwwroot/js/login.js | 94 ++++++++++- .../Bootstrap.DataAccess/Helper/SMSHelper.cs | 157 ++++++++++++++++++ .../Bootstrap.DataAccess/Helper/UserHelper.cs | 29 +++- 9 files changed, 394 insertions(+), 26 deletions(-) create mode 100644 src/admin/Bootstrap.DataAccess/Helper/SMSHelper.cs 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 @@