添加项目文件。
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt
|
||||
{
|
||||
/// <summary>
|
||||
/// 对称可逆加密
|
||||
/// </summary>
|
||||
public class CustomHSJWTService : ICustomJWTService
|
||||
{
|
||||
#region Option注入
|
||||
private readonly JWTTokenOptions _JWTTokenOptions;
|
||||
public CustomHSJWTService(IOptionsMonitor<JWTTokenOptions> jwtTokenOptions)
|
||||
{
|
||||
this._JWTTokenOptions = jwtTokenOptions.CurrentValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 用户登录成功以后,用来生成Token的方法
|
||||
/// </summary>
|
||||
/// <param name="UserName"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <returns></returns>
|
||||
public string GetToken(string UserName, string password)
|
||||
{
|
||||
#region 有效载荷,大家可以自己写,爱写多少写多少;尽量避免敏感信息
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, UserName),
|
||||
new Claim("NickName",UserName),
|
||||
new Claim("Role","Administrator"),//传递其他信息
|
||||
new Claim("ABCC","ABCC"),
|
||||
new Claim("ABCCDDDDD","ABCCDDDDD"),
|
||||
new Claim("Student","甜酱油")
|
||||
};
|
||||
|
||||
//需要加密:需要加密key:
|
||||
//Nuget引入:Microsoft.IdentityModel.Tokens
|
||||
SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_JWTTokenOptions.SecurityKey));
|
||||
|
||||
SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
//Nuget引入:System.IdentityModel.Tokens.Jwt
|
||||
JwtSecurityToken token = new JwtSecurityToken(
|
||||
issuer: _JWTTokenOptions.Issuer,
|
||||
audience: _JWTTokenOptions.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.Now.AddMinutes(5),//5分钟有效期
|
||||
signingCredentials: creds);
|
||||
|
||||
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
return returnToken;
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt
|
||||
{
|
||||
/// <summary>
|
||||
/// 非对称可逆加密
|
||||
/// </summary>
|
||||
public class CustomRSSJWTervice : ICustomJWTService
|
||||
|
||||
{
|
||||
#region Option注入
|
||||
private readonly JWTTokenOptions _JWTTokenOptions;
|
||||
public CustomRSSJWTervice(IOptionsMonitor<JWTTokenOptions> jwtTokenOptions)
|
||||
{
|
||||
this._JWTTokenOptions = jwtTokenOptions.CurrentValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public string GetToken(string userName, string password)
|
||||
{
|
||||
#region 使用加密解密Key 非对称
|
||||
string keyDir = Directory.GetCurrentDirectory();
|
||||
if (RSAHelper.TryGetKeyParameters(keyDir, true, out RSAParameters keyParams) == false)
|
||||
{
|
||||
keyParams = RSAHelper.GenerateAndSaveKey(keyDir);
|
||||
}
|
||||
#endregion
|
||||
|
||||
//string jtiCustom = Guid.NewGuid().ToString();//用来标识 Token
|
||||
Claim[] claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, userName),
|
||||
new Claim(ClaimTypes.Role,"admin"),
|
||||
new Claim("password",password)
|
||||
};
|
||||
|
||||
SigningCredentials credentials = new SigningCredentials(new RsaSecurityKey(keyParams), SecurityAlgorithms.RsaSha256Signature);
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: this._JWTTokenOptions.Issuer,
|
||||
audience: this._JWTTokenOptions.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.Now.AddMinutes(60),//5分钟有效期
|
||||
signingCredentials: credentials);
|
||||
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
string tokenString = handler.WriteToken(token);
|
||||
return tokenString;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt
|
||||
{
|
||||
public interface ICustomJWTService
|
||||
{
|
||||
string GetToken(string UserName, string password);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt
|
||||
{
|
||||
public class JWTTokenOptions
|
||||
{
|
||||
public string Audience
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public string SecurityKey
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
//public SigningCredentials Credentials
|
||||
//{
|
||||
// get;
|
||||
// set;
|
||||
//}
|
||||
public string Issuer
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt
|
||||
{
|
||||
public class RSAHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 从本地文件中读取用来签发 Token 的 RSA Key
|
||||
/// </summary>
|
||||
/// <param name="filePath">存放密钥的文件夹路径</param>
|
||||
/// <param name="withPrivate"></param>
|
||||
/// <param name="keyParameters"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TryGetKeyParameters(string filePath, bool withPrivate, out RSAParameters keyParameters)
|
||||
{
|
||||
string filename = withPrivate ? "key.json" : "key.public.json";
|
||||
string fileTotalPath = Path.Combine(filePath, filename);
|
||||
keyParameters = default(RSAParameters);
|
||||
if (!File.Exists(fileTotalPath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
keyParameters = JsonConvert.DeserializeObject<RSAParameters>(File.ReadAllText(fileTotalPath));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 生成并保存 RSA 公钥与私钥
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="withPrivate"></param>
|
||||
/// <returns></returns>
|
||||
public static RSAParameters GenerateAndSaveKey(string filePath, bool withPrivate = true)
|
||||
{
|
||||
RSAParameters publicKeys, privateKeys;
|
||||
using (var rsa = new RSACryptoServiceProvider(2048))//即时生成
|
||||
{
|
||||
try
|
||||
{
|
||||
privateKeys = rsa.ExportParameters(true);
|
||||
publicKeys = rsa.ExportParameters(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
rsa.PersistKeyInCsp = false;
|
||||
}
|
||||
}
|
||||
File.WriteAllText(Path.Combine(filePath, "key.json"), JsonConvert.SerializeObject(privateKeys));
|
||||
File.WriteAllText(Path.Combine(filePath, "key.public.json"), JsonConvert.SerializeObject(publicKeys));
|
||||
return withPrivate ? privateKeys : publicKeys;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user