零开始:NetCore项目权限管理系统:登录授权
喜欢NetCore的朋友,欢迎加群QQ:86594082
源码地址:https://github.com/feiyit/SoaProJect
管理员的模型
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新建一个管理员登录的接口
/// <summary> /// 管理员接口 /// </summary> public interface ISysAdminService { /// <summary> /// 登录 /// </summary> /// <param name="parm"></param> /// <returns></returns> Task<ApiResult<SysAdmin>> LoginAsync(SysAdminLogin parm); }
在创建一个实现类
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注入一下接口
services.AddTransient<ISysAdminService, SysAdminService>();
在Pages文件夹建一个FytAdmin的文件夹, Razor文件夹就是路径
创建一个Razor页面,名称为Login 选中生成PageModel类
前台使用Layui 可以节省大量工作量
在PageModel类中我们定义一个登录的方法
/// <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()) } });
这样一个登录的功能就实现了