You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.2 KiB
58 lines
2.2 KiB
3 years ago
|
using Binance.TradeRobot.Common.DI;
|
||
|
using Binance.TradeRobot.Common.Extensions;
|
||
|
using Binance.TradeRobot.Model.Base;
|
||
|
using Binance.TradeRobot.Model.Db;
|
||
|
using Binance.TradeRobot.Model.Dto;
|
||
|
using Microsoft.Extensions.Configuration;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.IdentityModel.Tokens;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IdentityModel.Tokens.Jwt;
|
||
|
using System.Security.Claims;
|
||
|
using System.Text;
|
||
|
|
||
|
namespace Binance.TradeRobot.Business
|
||
|
{
|
||
|
[BatchRegistration(ServiceLifetime.Singleton, RegistrationType.Self)]
|
||
|
public class UserBusiness : BaseBusiness
|
||
|
{
|
||
|
private IConfiguration configuration;
|
||
|
|
||
|
public UserBusiness(IFreeSql fsql, NLogManager logManager, IConfiguration configuration) : base(fsql, logManager)
|
||
|
{
|
||
|
this.configuration = configuration;
|
||
|
}
|
||
|
|
||
|
public LoginResponse Login(LoginRequest loginRequest)
|
||
|
{
|
||
|
var pwd = loginRequest.Pwd.ToMD5();
|
||
|
var user = fsql.Select<User>().Where(u => u.UserName == loginRequest.UserName && u.Pwd == pwd).ToOne();
|
||
|
if (user == null)
|
||
|
throw new BusinessException("用户名或密码错误");
|
||
|
|
||
|
#region 签发token
|
||
|
var claims = new List<Claim>()
|
||
|
{
|
||
|
new Claim("Id",user.Id.ToString()),
|
||
|
new Claim("UserName",user.UserName),
|
||
|
};
|
||
|
|
||
|
var jwtSecret = configuration.GetSection("JwtConfig:Secret").Value;
|
||
|
var jwtIssuer = configuration.GetSection("JwtConfig:Issuer").Value;
|
||
|
var jwtAudience = configuration.GetSection("JwtConfig:Audience").Value;
|
||
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret));
|
||
|
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||
|
var jwtToken = new JwtSecurityToken(jwtIssuer, jwtAudience, claims, expires: DateTime.Now.AddDays(30), signingCredentials: credentials);
|
||
|
var token = new JwtSecurityTokenHandler().WriteToken(jwtToken);
|
||
|
#endregion
|
||
|
return new LoginResponse()
|
||
|
{
|
||
|
Id = user.Id,
|
||
|
UserName = user.UserName,
|
||
|
Token = token
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|