添加项目文件。
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using IRaCIS.Application.Contracts;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
|
||||
namespace IRaCIS.Core.Application.Auth
|
||||
{
|
||||
public class IRaCISClaims
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string FullName { get; set; } = String.Empty;
|
||||
public string Code { get; set; } = String.Empty;
|
||||
public string RealName { get; set; } = String.Empty;
|
||||
|
||||
public string UserTypeShortName { get; set; } = String.Empty;
|
||||
|
||||
public UserTypeEnum UserTypeEnum { get; set; }
|
||||
|
||||
public string PermissionStr { get; set; } = String.Empty;
|
||||
|
||||
public Guid UserTypeId { get; set; }
|
||||
|
||||
public int IsAdmin { get; }
|
||||
|
||||
public string Phone { get; set; } = String.Empty;
|
||||
|
||||
public static IRaCISClaims Create(UserBasicInfo user)
|
||||
{
|
||||
return new IRaCISClaims
|
||||
{
|
||||
Id = user.Id,
|
||||
FullName = user.UserName,
|
||||
RealName = user.RealName,
|
||||
UserTypeEnum=user.UserTypeEnum,
|
||||
UserTypeId=user.UserTypeId,
|
||||
Code = user.Code,
|
||||
PermissionStr = user.PermissionStr,
|
||||
|
||||
UserTypeShortName = user.UserTypeShortName
|
||||
};
|
||||
}
|
||||
public static IRaCISClaims Create(DoctorAccountDTO doctor)
|
||||
{
|
||||
return new IRaCISClaims
|
||||
{
|
||||
Id = doctor.Id,
|
||||
FullName = doctor.FirstName + doctor.LastName,
|
||||
Phone = doctor.Phone,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace IRaCIS.Core.Application.Auth
|
||||
{
|
||||
public class JwtSetting
|
||||
{
|
||||
/// <summary>
|
||||
/// 颁发者
|
||||
/// </summary>
|
||||
public string Issuer { get; set; } = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 接收者
|
||||
/// </summary>
|
||||
public string Audience { get; set; } = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 令牌密码
|
||||
/// </summary>
|
||||
public string SecurityKey { get; set; } = String.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 过期时间
|
||||
/// </summary>
|
||||
public int TokenExpireDays { get; set; }
|
||||
|
||||
//public Dictionary<string, object> Claims { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 签名
|
||||
/// </summary>
|
||||
public SigningCredentials Credentials
|
||||
{
|
||||
get
|
||||
{
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecurityKey));
|
||||
return new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
using IRaCIS.Core.Infra.EFCore.AuthUser;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace IRaCIS.Core.Application.Auth
|
||||
{
|
||||
|
||||
public interface ITokenService
|
||||
{
|
||||
string GetToken(IRaCISClaims user);
|
||||
}
|
||||
|
||||
|
||||
public class TokenService : ITokenService
|
||||
{
|
||||
private readonly JwtSetting _jwtSetting;
|
||||
|
||||
public TokenService(IOptions<JwtSetting> option)
|
||||
{
|
||||
_jwtSetting = option.Value;
|
||||
}
|
||||
|
||||
public string GetToken(IRaCISClaims user)
|
||||
{
|
||||
//创建用户身份标识,可按需要添加更多信息
|
||||
var claims = new Claim[]
|
||||
{
|
||||
new Claim(Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
new Claim(JwtIRaCISClaimType.Id, user.Id.ToString()),
|
||||
new Claim(JwtIRaCISClaimType.Name, user.FullName),
|
||||
new Claim(JwtIRaCISClaimType.RealName, user.RealName),
|
||||
new Claim(JwtIRaCISClaimType.Code,user.Code),
|
||||
new Claim(JwtIRaCISClaimType.UserTypeId,user.UserTypeId.ToString()),
|
||||
new Claim(JwtIRaCISClaimType.UserTypeEnum,user.UserTypeEnum.ToString()),
|
||||
new Claim(JwtIRaCISClaimType.UserTypeEnumInt,((int)user.UserTypeEnum).ToString()),
|
||||
new Claim(JwtIRaCISClaimType.UserTypeShortName,user.UserTypeShortName),
|
||||
new Claim(JwtIRaCISClaimType.PermissionStr,user.PermissionStr)
|
||||
};
|
||||
|
||||
////创建令牌
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: _jwtSetting.Issuer,
|
||||
audience: _jwtSetting.Audience,
|
||||
signingCredentials: _jwtSetting.Credentials,
|
||||
claims: claims,
|
||||
notBefore: DateTime.Now,
|
||||
expires: DateTime.Now.AddDays(_jwtSetting.TokenExpireDays)
|
||||
);
|
||||
|
||||
string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
return jwtToken;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user