158 lines
4.6 KiB
C#
158 lines
4.6 KiB
C#
using System.Text;
|
|
using System.Transactions;
|
|
using Common;
|
|
using CommonEntity;
|
|
using LogCap.Common;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NLog;
|
|
|
|
namespace EMQX_HttpAuth.Controllers
|
|
{
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class ValuesController : ControllerBase
|
|
{
|
|
|
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
|
|
|
[HttpPost()]
|
|
public Resp Validate([FromBody] UserData data)
|
|
{
|
|
Resp r = new Resp();
|
|
try
|
|
{
|
|
if (data.username.Equals("admin") && data.password.Equals("@8xq*BU3+3kuE:Oj"))
|
|
{
|
|
r.result = "allow";
|
|
r.is_superuser = false;
|
|
}
|
|
else
|
|
{
|
|
string Key = CacheKey.MQTTValidatePrefix + data.username;
|
|
string password = CSRedisCacheHelper.Get_Partition<string>(Key, 1);
|
|
if (!string.IsNullOrEmpty(password))
|
|
{
|
|
if (data.password.Equals(password))
|
|
{
|
|
r.result = "allow";
|
|
r.is_superuser = false;
|
|
}
|
|
else
|
|
{
|
|
_logger.Error("deny 111: " + data.username + " " + data.password);
|
|
r.result = "deny";
|
|
r.is_superuser = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_logger.Error("deny 222: " + data.username + " " + data.password);
|
|
r.result = "deny";
|
|
r.is_superuser = false;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error("chu cuo: " + ex.Message);
|
|
r.result = "deny";
|
|
r.is_superuser = false;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
[HttpPost()]
|
|
public ReturnInfo SetChache(List<UserData> datalist)
|
|
{
|
|
ReturnInfo r = new ReturnInfo();
|
|
try
|
|
{
|
|
foreach (var data in datalist)
|
|
{
|
|
string Key = CacheKey.MQTTValidatePrefix + data.username;
|
|
CSRedisCacheHelper.Set_Partition<string>(Key, data.password, 1);
|
|
}
|
|
r.isok = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex.Message);
|
|
r.isok = false;
|
|
r.message = ex.Message;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置设备的ClientId对应的Key
|
|
/// </summary>
|
|
/// <param name="datalist"></param>
|
|
/// <returns></returns>
|
|
[HttpPost()]
|
|
public ReturnInfo SetDeviceInfoChache(List<DeviceCacheInfo> datalist)
|
|
{
|
|
string Key =CacheKey.DeviceInfo;
|
|
ReturnInfo r = new ReturnInfo();
|
|
try
|
|
{
|
|
foreach (var data in datalist)
|
|
{
|
|
CSRedisCacheHelper.HMSet(0, Key, data.ClientId, data.SecretKey);
|
|
}
|
|
r.isok = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex.Message);
|
|
r.isok = false;
|
|
r.message = ex.Message;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
|
|
|
|
[HttpPost()]
|
|
public ReturnInfo DelCache(List<UserData> data)
|
|
{
|
|
ReturnInfo r = new ReturnInfo();
|
|
try
|
|
{
|
|
var q = data.Select(A => A.username).ToArray();
|
|
CSRedisCacheHelper.Del_Partition(q, 1);
|
|
r.isok = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
r.isok = false;
|
|
r.message = ex.Message;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
public void MonitorMQTTClient()
|
|
{
|
|
var readResult = Request.BodyReader.ReadAsync().Result;
|
|
Request.BodyReader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
|
|
string body = Encoding.UTF8.GetString(readResult.Buffer.FirstSpan);
|
|
}
|
|
public void GenerateSecret()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
public class UserData
|
|
{
|
|
public string username { get; set; }
|
|
public string password { get; set; }
|
|
}
|
|
public class Resp
|
|
{
|
|
public string result { get; set; }
|
|
public bool is_superuser { get; set; }
|
|
}
|
|
}
|
|
|