喜歡NetCore的朋友,歡迎加羣QQ:86594082html
源碼地址:https://github.com/feiyit/SoaProJectjquery
管理員的模型git
namespace FytSoa.Core.Model.Sys { public class SysAdmin { /// <summary> /// 惟一編號 /// </summary> public string Guid { get; set; } /// <summary> /// 歸屬角色 /// </summary> public string RoleGuid { get; set; } /// <summary> /// 歸屬部門 /// </summary> public string DepartmentGuid { get; set; } /// <summary> /// 登陸帳號 /// </summary> public string LoginName { get; set; } /// <summary> /// 登陸密碼 /// </summary> public string LoginPwd { get; set; } /// <summary> /// 真是姓名 /// </summary> public string TrueName { get; set; } /// <summary> /// 編號 /// </summary> public string Number { get; set; } /// <summary> /// 頭像 /// </summary> public string HeadPic { get; set; } /// <summary> /// 性別 /// </summary> public string Sex { get; set; } = "男"; /// <summary> /// 手機號碼 /// </summary> public string Mobile { get; set; } /// <summary> /// 狀態 1=整除 0=不容許登陸 /// </summary> public bool Status { get; set; } /// <summary> /// 郵箱 /// </summary> public string Email { get; set; } /// <summary> /// 備註 /// </summary> public string Summary { get; set; } /// <summary> /// 添加時間 /// </summary> public DateTime AddDate { get; set; } = DateTime.Now; /// <summary> /// 當前登陸時間 /// </summary> public DateTime? LoginDate { get; set; } /// <summary> /// 上次登陸時間 /// </summary> public DateTime? UpLoginDate { get; set; } } }
在FytSoa.Service新建一個管理員登陸的接口github
/// <summary> /// 管理員接口 /// </summary> public interface ISysAdminService { /// <summary> /// 登陸 /// </summary> /// <param name="parm"></param> /// <returns></returns> Task<ApiResult<SysAdmin>> LoginAsync(SysAdminLogin parm); }
在建立一個實現類ajax
public class SysAdminService : DbContext, ISysAdminService { /// <summary> /// 用戶登陸實現 /// </summary> /// <param name="parm"></param> /// <returns></returns> public async Task<ApiResult<SysAdmin>> LoginAsync(SysAdminLogin parm) { var res = new ApiResult<SysAdmin>(); try { parm.password = DES3Encrypt.EncryptString(parm.password); var model = Db.Queryable<SysAdmin>() .Where(m => m.LoginName == parm.loginname).First(); if (model != null) { if (model.LoginPwd.Equals(parm.password)) { res.success = true; res.message = "獲取成功!"; res.data = model; } else { res.success = false; res.statusCode = (int)ApiEnum.Error; res.message = "密碼錯誤~"; } } else { res.success = false; res.statusCode = (int)ApiEnum.Error; res.message = "帳號錯誤~"; } } catch (Exception ex) { res.message = ApiEnum.Error.GetEnumText() + ex.Message; res.statusCode = (int)ApiEnum.Error; } return await Task.Run(() => res); } }
在Startup注入一下接口api
services.AddTransient<ISysAdminService, SysAdminService>();
在Pages文件夾建一個FytAdmin的文件夾, Razor文件夾就是路徑async
建立一個Razor頁面,名稱爲Login 選中生成PageModel類ide
前臺使用Layui 能夠節省大量工做量post
在PageModel類中咱們定義一個登陸的方法ui
/// <summary> /// 登陸 /// </summary> /// <param name="parm"></param> /// <returns></returns> public async Task<IActionResult> OnPostLoginAsync(SysAdminLogin parm) { var apiRes = _sysAdminService.LoginAsync(parm); try { var user = apiRes.Result.data; if (apiRes.Result.statusCode == 200) { var identity = new ClaimsPrincipal( new ClaimsIdentity(new[] { new Claim(ClaimTypes.Sid,user.Guid), new Claim(ClaimTypes.Role,"超級管理員"), new Claim(ClaimTypes.Thumbprint,user.HeadPic), new Claim(ClaimTypes.Name,user.TrueName), new Claim(ClaimTypes.UserData,user.UpLoginDate.ToString()), }, CookieAuthenticationDefaults.AuthenticationScheme) ); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, identity, new AuthenticationProperties { ExpiresUtc = DateTime.UtcNow.AddMinutes(60), IsPersistent = true, AllowRefresh = false }); } } catch (Exception ex) { apiRes.Result.message = ex.Message; apiRes.Result.statusCode = (int)ApiEnum.Error; } return new JsonResult(new ApiResult<string>() { statusCode = apiRes.Result.statusCode, message = apiRes.Result.message }); }
這樣服務端的方法就寫好了,關於Core2.0的登陸受權,能夠移步不到 Asp .Net Core 2.0 登陸受權以及多用戶登陸
新建一個Login.js
layui.use(['element', 'jquery', 'form'], function () { var form = layui.form, layer = layui.layer, $ = layui.jquery; form.on('submit(loginsub)', function (data) { console.log(data.field); $.ajax({ type: "post", url: "/fytadmin/login?handler=login", data: data.field, success: function (res) { if (res.statusCode == 200) { window.location.href = '/fytadmin/index'; } else { alert(res.message); } } }) return false; }); $(window).resize( bodysize ); bodysize(); function bodysize() { $("body").height($(window).height()) } });
這樣一個登陸的功能就實現了