采用强类型模型改造请求参数
This commit is contained in:
parent
58ead0037c
commit
9fa84bc498
|
@ -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; }
|
||||
|
||||
/// <summary>应用登录</summary>
|
||||
/// <param name="user">应用名</param>
|
||||
/// <param name="pass"></param>
|
||||
/// <param name="displayName">显示名</param>
|
||||
/// <param name="machine">机器码</param>
|
||||
/// <param name="processId">进程Id</param>
|
||||
/// <param name="version">版本</param>
|
||||
/// <param name="compile">编译时间</param>
|
||||
/// <param name="model">模型</param>
|
||||
/// <returns></returns>
|
||||
[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)
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AntJob.Models
|
||||
{
|
||||
/// <summary>登录模型</summary>
|
||||
public class LoginModel
|
||||
{
|
||||
/// <summary>用户名</summary>
|
||||
public String User { get; set; }
|
||||
|
||||
/// <summary>用户名</summary>
|
||||
public String Pass { get; set; }
|
||||
|
||||
/// <summary>显示名</summary>
|
||||
public String DisplayName { get; set; }
|
||||
|
||||
/// <summary>机器名</summary>
|
||||
public String Machine { get; set; }
|
||||
|
||||
/// <summary>进程Id</summary>
|
||||
public Int32 ProcessId { get; set; }
|
||||
|
||||
/// <summary>版本</summary>
|
||||
public String Version { get; set; }
|
||||
|
||||
/// <summary>编译时间</summary>
|
||||
public DateTime Compile { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>登录响应</summary>
|
||||
public class LoginResponse
|
||||
{
|
||||
/// <summary>名称</summary>
|
||||
public String Name { get; set; }
|
||||
|
||||
/// <summary>密钥。仅注册时返回</summary>
|
||||
public String Secret { get; set; }
|
||||
|
||||
/// <summary>显示名</summary>
|
||||
public String DisplayName { get; set; }
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
|
||||
/// <summary>最后一次登录成功后的消息</summary>
|
||||
public IDictionary<String, Object> Info { get; private set; }
|
||||
public LoginResponse Info { get; private set; }
|
||||
#endregion
|
||||
|
||||
#region 方法
|
||||
|
@ -79,26 +80,26 @@ namespace AntJob.Providers
|
|||
var des = asmx?.Asm.GetCustomAttribute<DescriptionAttribute>();
|
||||
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<IDictionary<String, Object>>(client, "Login", arg);
|
||||
var rs = await base.InvokeWithClientAsync<LoginResponse>(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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue