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.
79 lines
2.4 KiB
79 lines
2.4 KiB
10 months ago
|
using HuiXin.Gateway.Ocelot.Extensions;
|
||
|
using Serilog;
|
||
|
using System.Security.Cryptography;
|
||
|
|
||
|
namespace HuiXin.Gateway.Ocelot
|
||
|
{
|
||
|
public class Program
|
||
|
{
|
||
|
public static void Main(string[] args)
|
||
|
{
|
||
|
//BuildJwt();
|
||
|
|
||
|
var configuration = new ConfigurationBuilder()
|
||
|
.AddJsonFile("Configs/ocelot.json", false, true)
|
||
|
.AddJsonFile("Configs/jwt.json", false, false)
|
||
|
.AddJsonFile("Configs/serilog.json", false, true)
|
||
|
.Build();
|
||
|
|
||
|
var logger = new LoggerConfiguration()
|
||
|
.ReadFrom.Configuration(configuration)
|
||
|
.CreateLogger();
|
||
|
Log.Logger = logger;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
||
|
builder.Configuration.AddConfiguration(configuration);
|
||
|
|
||
|
var services = builder.Services;
|
||
|
|
||
|
services.AddCors();
|
||
|
services.AddLogging(loggingBuilder => loggingBuilder.ClearProviders().AddSerilog());
|
||
|
|
||
|
services.AddJWT(configuration);
|
||
|
|
||
|
services.AddMyOcelot(configuration);
|
||
|
|
||
|
var app = builder.Build();
|
||
|
|
||
|
app.UseCors();
|
||
|
app.UseJWT();
|
||
|
|
||
|
app.UseMyOcelot();
|
||
|
|
||
|
app.Run();
|
||
|
|
||
|
Log.Information("ϵͳ������");
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Fatal(ex, "ϵͳ�쳣");
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
Log.CloseAndFlush();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void BuildJwt()
|
||
|
{
|
||
|
using var aes = Aes.Create();
|
||
|
aes.KeySize = 256;
|
||
|
aes.GenerateKey();
|
||
|
var sign = Convert.ToBase64String(aes.Key);
|
||
|
var extraHeaders = new Dictionary<string, string> { { "kid", "MyKey" } };
|
||
|
//����ʱ��(���Բ����ã�������ʾǩ���� 10������)
|
||
|
double exp = (DateTime.UtcNow.AddDays(10) - new DateTime(1970, 1, 1)).TotalSeconds;
|
||
|
var payload = new Dictionary<string, object>
|
||
|
{
|
||
|
{ "userId", "001" },
|
||
|
{ "userAccount", "fan" },
|
||
|
{ "exp",exp }
|
||
|
};
|
||
|
Console.WriteLine("Token��" + JWTUtil.CreateJwtToken(payload, sign, extraHeaders));
|
||
|
}
|
||
|
}
|
||
|
}
|