145 lines
4.5 KiB
C#
145 lines
4.5 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using CommonEntity;
|
|
using IotManager.Common;
|
|
using IotManager.private_key;
|
|
using Jose;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using MySQLAccess.PGModels;
|
|
using NLog;
|
|
using ViewModels;
|
|
|
|
namespace IotManager.Controllers
|
|
{
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class LoginController : ControllerBase
|
|
{
|
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
|
public IConfiguration? configuration { get; set; }
|
|
|
|
private PostgresContext IotServerContext { get; set; }
|
|
public LoginController(IConfiguration _configuration, PostgresContext iot)
|
|
{
|
|
configuration = _configuration;
|
|
this.IotServerContext = iot;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 登录
|
|
/// </summary>
|
|
/// <param name="username">用户名</param>
|
|
/// <param name="password">密码</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public ReturnInfo Login([FromBody] LoginData data)
|
|
{
|
|
|
|
ReturnInfo res = new ReturnInfo();
|
|
|
|
try
|
|
{
|
|
Userinfo? entity = null;
|
|
string password = data.password;
|
|
string username = data.username;
|
|
string TokenString = "";
|
|
//using (var q = new IotServerContext())
|
|
//{
|
|
entity = IotServerContext.Userinfos.SingleOrDefault(A => A.UserName.Equals(username));
|
|
if (entity != null)
|
|
{
|
|
bool vvv = JiaJieMi.VerifyHashedPassword(entity.PassWord, password);
|
|
if (vvv == false)
|
|
{
|
|
res.isok = false;
|
|
res.message = "密码错误";
|
|
}
|
|
else
|
|
{
|
|
TokenString = GetToken(entity);
|
|
res.isok = true;
|
|
|
|
|
|
ResLoginData r = new ResLoginData();
|
|
r.AccessToken = TokenString;
|
|
r.Id = entity.Id;
|
|
r.Permission = entity.Permission;
|
|
r.UserName = entity.UserName;
|
|
r.RealName = entity.RealName;
|
|
r.CompanyName = entity.CompanyName;
|
|
|
|
res.response = r;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
res.isok = false;
|
|
res.message = "用户不存在";
|
|
}
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
res.message = ex.Message;
|
|
res.isok = false;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public string GetToken([FromBody] Userinfo? entity)
|
|
{
|
|
string TokenString;
|
|
var claims = new Claim[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, entity.Id.ToString()),
|
|
new Claim(ClaimTypes.Name, entity.UserName),
|
|
new Claim(ClaimTypes.Hash,Guid.NewGuid().ToString("N"))
|
|
};
|
|
|
|
var secretByte = Encoding.UTF8.GetBytes(configuration["JwT:SecretKey"]);
|
|
var signingKey = new SymmetricSecurityKey(secretByte);
|
|
var a = SecurityAlgorithms.HmacSha256;
|
|
|
|
var signingCredentials = new SigningCredentials(signingKey, a);
|
|
|
|
//有效期设置为1天signingCredentials //数字名
|
|
var token = new JwtSecurityToken(
|
|
issuer: configuration?["JwT:Issuer"],
|
|
audience: configuration?["JwT:Audience"],//接收
|
|
claims: claims,//存放的用户信息
|
|
notBefore: DateTime.UtcNow,//发布时间
|
|
expires: DateTime.UtcNow.AddHours(12),
|
|
signingCredentials: signingCredentials
|
|
);
|
|
TokenString = new JwtSecurityTokenHandler().WriteToken(token);
|
|
return TokenString;
|
|
}
|
|
|
|
[HttpPost()]
|
|
[Authorize()]
|
|
public string Helloooo(string key)
|
|
{
|
|
return "allow";
|
|
}
|
|
|
|
|
|
[HttpPost()]
|
|
public string MyTTT(DengLu key)
|
|
{
|
|
return "allow";
|
|
}
|
|
}
|
|
public class DengLu
|
|
{
|
|
|
|
public string UserName { get; set; }
|
|
public string PassWord { get; set; }
|
|
}
|
|
}
|