add(refreshTokenService):完善刷新令牌服务
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
|
||||
using IM_API.Configs;
|
||||
using IM_API.Models;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using StackExchange.Redis;
|
||||
using System.Text;
|
||||
|
||||
namespace IM_API
|
||||
{
|
||||
@@ -16,14 +20,74 @@ namespace IM_API
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.Build();
|
||||
string conStr = builder.Configuration.GetConnectionString("DefaultConnection")!;
|
||||
|
||||
string conStr = configuration.GetConnectionString("DefaultConnection")!;
|
||||
string redisConStr = configuration.GetConnectionString("Redis");
|
||||
//注入数据库上下文
|
||||
builder.Services.AddDbContext<ImContext>(options =>
|
||||
{
|
||||
options.UseMySql(conStr,ServerVersion.AutoDetect(conStr));
|
||||
});
|
||||
//注入redis
|
||||
var redis = ConnectionMultiplexer.Connect(redisConStr);
|
||||
builder.Services.AddSingleton<IConnectionMultiplexer>(redis);
|
||||
|
||||
builder.Services.AddAllService(configuration);
|
||||
|
||||
builder.Services.AddSignalR();
|
||||
//允许所有来源(跨域)
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyOrigin();
|
||||
});
|
||||
});
|
||||
//凭证处理
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
//https非必须
|
||||
options.RequireHttpsMetadata = false;
|
||||
//保存token
|
||||
options.SaveToken = true;
|
||||
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
||||
{
|
||||
//验证签发者
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = configuration["Jwt:Issuer"],
|
||||
//验证受众
|
||||
ValidateAudience = true,
|
||||
ValidAudience = configuration["Jwt:Audience"],
|
||||
//验证签名密钥
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"])),
|
||||
//时间偏差容忍
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.FromSeconds(30)
|
||||
|
||||
|
||||
};
|
||||
//websocket token凭证处理
|
||||
options.Events = new JwtBearerEvents {
|
||||
OnMessageReceived = context =>
|
||||
{
|
||||
var accessToken = context.Request.Query["access_token"];
|
||||
if (!string.IsNullOrEmpty(accessToken))
|
||||
{
|
||||
context.Token = accessToken;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
@@ -41,6 +105,7 @@ namespace IM_API
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
app.UseAuthentication();
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
Reference in New Issue
Block a user