From 9fa84bc498bcde38d901b6b6df6aea9cc145f172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Mon, 7 Sep 2020 21:51:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E7=94=A8=E5=BC=BA=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=94=B9=E9=80=A0=E8=AF=B7=E6=B1=82=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AntJob.Server/AntService.cs | 66 ++++++++++++----------------------- AntJob/Models/LoginModel.cs | 46 ++++++++++++++++++++++++ AntJob/Providers/AntClient.cs | 23 ++++++------ 3 files changed, 80 insertions(+), 55 deletions(-) create mode 100644 AntJob/Models/LoginModel.cs diff --git a/AntJob.Server/AntService.cs b/AntJob.Server/AntService.cs index b32a658..ae1bfe8 100644 --- a/AntJob.Server/AntService.cs +++ b/AntJob.Server/AntService.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using AntJob.Data; using AntJob.Data.Entity; +using AntJob.Models; using NewLife; using NewLife.Data; using NewLife.Log; @@ -28,84 +29,61 @@ namespace AntJob.Server public IApiSession Session { get; set; } /// 应用登录 - /// 应用名 - /// - /// 显示名 - /// 机器码 - /// 进程Id - /// 版本 - /// 编译时间 + /// 模型 /// [Api(nameof(Login))] - public Object Login(String user, String pass, String displayName, String machine, Int32 processId, String version, DateTime compile) + public LoginResponse Login(LoginModel model) { - if (user.IsNullOrEmpty()) throw new ArgumentNullException(nameof(user)); - //if (pass.IsNullOrEmpty()) throw new ArgumentNullException(nameof(pass)); - - //var ps = ControllerContext.Current.Parameters; - //var displayName = ps["DisplayName"] + ""; - //var machine = ps["machine"] + ""; - //var processid = ps["processid"].ToInt(); - //var version = ps["version"] + ""; - //var compile = ps["Compile"].ToDateTime(); + if (model.User.IsNullOrEmpty()) throw new ArgumentNullException(nameof(model.User)); var ns = Session as INetSession; var ip = ns.Remote.Host; - WriteLog("[{0}]从[{1}]登录[{2}@{3}]", user, ns.Remote, machine, processId); + WriteLog("[{0}]从[{1}]登录[{2}@{3}]", model.User, ns.Remote, model.Machine, model.ProcessId); // 找应用 var autoReg = false; - var app = App.FindByName(user); - if (app == null || app.Secret.MD5() != pass) + var app = App.FindByName(model.User); + if (app == null || app.Secret.MD5() != model.Pass) { - app = CheckApp(app, user, pass, ip); - if (app == null) throw new ArgumentOutOfRangeException(nameof(user)); + app = CheckApp(app, model.User, model.Pass, ip); + if (app == null) throw new ArgumentOutOfRangeException(nameof(model.User)); autoReg = true; } - if (app == null) throw new Exception($"应用[{user}]不存在!"); + if (app == null) throw new Exception($"应用[{model.User}]不存在!"); if (!app.Enable) throw new Exception("已禁用!"); // 核对密码 if (!autoReg && !app.Secret.IsNullOrEmpty()) { var pass2 = app.Secret.MD5(); - if (pass != pass2) throw new Exception("密码错误!"); + if (model.Pass != pass2) throw new Exception("密码错误!"); } // 版本和编译时间 - if (app.Version.IsNullOrEmpty() || app.Version.CompareTo(version) < 0) app.Version = version; - if (app.CompileTime < compile) app.CompileTime = compile; - if (app.DisplayName.IsNullOrEmpty()) app.DisplayName = displayName; + if (app.Version.IsNullOrEmpty() || app.Version.CompareTo(model.Version) < 0) app.Version = model.Version; + if (app.CompileTime < model.Compile) app.CompileTime = model.Compile; + if (app.DisplayName.IsNullOrEmpty()) app.DisplayName = model.DisplayName; app.Save(); // 应用上线 - var online = CreateOnline(app, ns, machine, processId); - online.Version = version; - online.CompileTime = compile; + var online = CreateOnline(app, ns, model.Machine, model.ProcessId); + online.Version = model.Version; + online.CompileTime = model.Compile; online.Save(); // 记录当前用户 Session["App"] = app; - WriteHistory(autoReg ? "注册" : "登录", true, $"[{user}/{pass}]在[{machine}@{processId}]登录[{app}]成功"); + WriteHistory(autoReg ? "注册" : "登录", true, $"[{model.User}/{model.Pass}]在[{model.Machine}@{model.ProcessId}]登录[{app}]成功"); - if (autoReg) - return new - { - app.Name, - app.Secret, - app.DisplayName, - }; - else - return new - { - app.Name, - app.DisplayName, - }; + var rs = new LoginResponse { Name = app.Name, DisplayName = app.DisplayName }; + if (autoReg) rs.Secret = app.Secret; + + return rs; } protected virtual App CheckApp(App app, String user, String pass, String ip) diff --git a/AntJob/Models/LoginModel.cs b/AntJob/Models/LoginModel.cs new file mode 100644 index 0000000..b850f44 --- /dev/null +++ b/AntJob/Models/LoginModel.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntJob.Models +{ + /// 登录模型 + public class LoginModel + { + /// 用户名 + public String User { get; set; } + + /// 用户名 + public String Pass { get; set; } + + /// 显示名 + public String DisplayName { get; set; } + + /// 机器名 + public String Machine { get; set; } + + /// 进程Id + public Int32 ProcessId { get; set; } + + /// 版本 + public String Version { get; set; } + + /// 编译时间 + public DateTime Compile { get; set; } + } + + /// 登录响应 + public class LoginResponse + { + /// 名称 + public String Name { get; set; } + + /// 密钥。仅注册时返回 + public String Secret { get; set; } + + /// 显示名 + public String DisplayName { get; set; } + } +} \ No newline at end of file diff --git a/AntJob/Providers/AntClient.cs b/AntJob/Providers/AntClient.cs index 429e167..a670b8d 100644 --- a/AntJob/Providers/AntClient.cs +++ b/AntJob/Providers/AntClient.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Reflection; using System.Threading.Tasks; using AntJob.Data; +using AntJob.Models; using NewLife; using NewLife.Log; using NewLife.Net; @@ -29,7 +30,7 @@ namespace AntJob.Providers public Boolean Logined { get; set; } /// 最后一次登录成功后的消息 - public IDictionary Info { get; private set; } + public LoginResponse Info { get; private set; } #endregion #region 方法 @@ -79,26 +80,26 @@ namespace AntJob.Providers var des = asmx?.Asm.GetCustomAttribute(); var dname = title?.Title ?? dis?.DisplayName ?? des?.Description; - var arg = new + var arg = new LoginModel { - user = UserName, - pass = Password.IsNullOrEmpty() ? null : Password.MD5(), + User = UserName, + Pass = Password.IsNullOrEmpty() ? null : Password.MD5(), DisplayName = dname, - machine = Environment.MachineName, - processid = Process.GetCurrentProcess().Id, - version = asmx?.Version, - asmx?.Compile, + Machine = Environment.MachineName, + ProcessId = Process.GetCurrentProcess().Id, + Version = asmx.Version, + Compile = asmx.Compile, }; - var rs = await base.InvokeWithClientAsync>(client, "Login", arg); + var rs = await base.InvokeWithClientAsync(client, "Login", arg); var set = AntSetting.Current; if (set.Debug) XTrace.WriteLine("登录{0}成功!{1}", client, rs.ToJson()); // 保存下发密钥 - if (rs.TryGetValue("Secret", out var secret)) + if (!rs.Secret.IsNullOrEmpty()) { - set.Secret = secret + ""; + set.Secret = rs.Secret; set.Save(); }