接入 用户中心的鉴权Token

删除 班级 学校接口 [使用用户中心接口]
This commit is contained in:
小肥羊 2025-08-14 18:25:05 +08:00
parent 3087f40316
commit eb0479552c
13 changed files with 39 additions and 78 deletions

View File

@ -66,6 +66,7 @@ namespace Learn.Archives.API.Controllers
AccessToken = JwtHelper.GetToken(AppCommon.Config.AuthKey,
[
new Claim(ClaimEnum.Role,admin.RoleId.ToString()),
new Claim(ClaimEnum.UserId,admin.RoleId.ToString()),
new Claim(ClaimEnum.Id, admin.Id.ToString()),
new Claim(ClaimEnum.Name, admin.Name),
])

View File

@ -1,35 +0,0 @@
using Learn.Archives.API.Controllers.Dto;
using Learn.Archives.API.Expand;
using Learn.Archives.Core.Common;
using Learn.Archives.Core.Model;
using Learn.Archives.Core.Model.Dto;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;
using System.Security.Claims;
using UserCenter.Model;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Learn.Archives.API.Controllers
{
/// <summary>
/// 班级控制器
/// </summary>
public class ClassController : BackController<Classes>
{
readonly Repository<Classes> baseService;
readonly LiveUserInfo userInfo;
public ClassController(Repository<Classes> baseService, LiveUserInfo userInfo) : base(baseService)
{
this.baseService = baseService;
this.userInfo = userInfo;
}
public override async Task<dynamic> PageList([FromBody] QueryRequestBase model)
{
var res = (PageResult<Classes>) await base.PageList(model);
return new PageResult<ClassDto>() { Data = res.Data.Adapt<List<ClassDto>>(), Total = res.Total };
}
}
}

View File

@ -1,26 +0,0 @@
using Learn.Archives.API.Controllers.Dto;
using Learn.Archives.API.Expand;
using Learn.Archives.Core.Common;
using Learn.Archives.Core.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Security.Claims;
using UserCenter.Model;
namespace Learn.Archives.API.Controllers
{
public class SchoolController : BackController<School>
{
readonly Repository<School> baseService;
public SchoolController(Repository<School> baseService) : base(baseService)
{
this.baseService = baseService;
}
public override Task<bool> Del([FromBody] params long[] ids)
{
return base.Del(ids);
}
}
}

View File

@ -45,7 +45,8 @@ namespace Learn.Archives.API.Controllers
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[HttpGet()]
[Route("/api/[controller]/{id}")]
public virtual async Task<dynamic> Info(long id)
{
return await _baseRepository.GetByIdAsync(id);
@ -139,8 +140,8 @@ namespace Learn.Archives.API.Controllers
[HttpPost]
public virtual async Task<List<ComboModel>> QueryCombo([FromBody] QueryCombo model)
{
if (string.IsNullOrEmpty(model.ValueName) || string.IsNullOrEmpty(model.TextName))
Oh.ModelError("ValueName TextName 是必填项");
//if (string.IsNullOrEmpty(model.ValueName) || string.IsNullOrEmpty(model.TextName))
// Oh.ModelError("ValueName TextName 是必填项");
var sqlquery = BaseQuery(model);
var res = await sqlquery.Select<ComboModel>($"{model.TextName} as Text , {model.ValueName} as Value").ToListAsync();
return res;

View File

@ -20,6 +20,8 @@ namespace Learn.Archives.API.Expand
{
options.RequireHttpsMetadata = false;
options.UseSecurityTokenValidators = true;
options.MapInboundClaims = false; // .NET 5+
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
options.TokenValidationParameters = new TokenValidationParameters
{
SaveSigninToken = false,//保存token,后台验证token是否生效(重要)
@ -47,18 +49,14 @@ namespace Learn.Archives.API.Expand
},
OnAuthenticationFailed = context =>
{
context.Response.Clear();
context.Response.ContentType = "application/json";
context.Response.StatusCode = 403;
var data = new BaseReturn() { Code = 403, Message = context.Exception.Message + context.Exception?.StackTrace };
context.Response.WriteAsync(data.ToJson());
return Task.CompletedTask;
},
OnChallenge = context =>
{
if(context.Response.StatusCode == 403 || context.Response.StatusCode == 401)
return Task.CompletedTask;
context.HandleResponse();
if (context.Response.StatusCode == 403)
return Task.CompletedTask;
context.Response.Clear();
context.Response.ContentType = "application/json";
context.Response.StatusCode = 401;

View File

@ -55,9 +55,10 @@ app.UseRouting();
//自定义 应用
app.UseCorsExpand();
app.UseAuthorization();
app.UseSqlSugarExpand();
app.UseAuthorization();
app.MapControllers();

View File

@ -17,7 +17,7 @@
"UpdateTable": false
},
"AuthKey": {
"Secret": "9FAB7AC7-F2DB-4C52-B81F-044055A34AF2",
"Secret": "9FAB7AC7-F1DB-4C56-B84F-044055A34AF2",
"Issuer": "Learn.Archive", //
"Audience": "Admin",
"Expires": 120 // 120

View File

@ -70,8 +70,22 @@ namespace Learn.Archives.Core.Common
/// </summary>
public SimpLetexConfig SimpLetex { get; set; } = new SimpLetexConfig();
}
/// <summary>
/// 用户中心服务配置
/// </summary>
public UserCenterServiceConfig UserCenterService { get; set; } = new UserCenterServiceConfig();
}
/// <summary>
/// 用户中心服务配置
/// </summary>
public class UserCenterServiceConfig
{
/// <summary>
/// api请求地址
/// </summary>
public string API { get; set; }
}
public class AuthKeyConfig
{
/// <summary>

View File

@ -8,6 +8,6 @@ namespace Learn.Archives.Core.Common
{
public class Authentication
{
public const string Admin = "Admin";
public const string Admin = "admin";
}
}

View File

@ -6,7 +6,7 @@
public static string PositionId => "position";
public static string UserId => "user";
public static string Id => "id";
public static string Role => "roleid";
public static string Role => "role";
public static string Scope => "scope";
public static string Name => "name";
}

View File

@ -26,6 +26,7 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="SqlSugar.IOC" Version="2.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.170" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="UserCenter.Model" Version="1.4.1" />
</ItemGroup>
</Project>

View File

@ -45,13 +45,13 @@ namespace Learn.Archives.Core.Model.Dto
/// <summary>
/// 值对应属性名称
/// </summary>
[Required(ErrorMessage = "{0}是必填项")]
public string ValueName { get; set; }
[Required(ErrorMessage = "{0}是必填项")]
public string ValueName { get; set; } = "Id";
/// <summary>
/// 文本对应属性名称
/// </summary>
[Required(ErrorMessage = "{0}是必填项")]
public string TextName { get; set; }
public string TextName { get; set; } = "Name";
}
/// <summary>

View File

@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Text.Json;
using UserCenter.Model;
using UserCenter.Model.Common;
using UserCenter.Model.Enum;
using UserCenter.Model.Interface;
@ -26,6 +27,11 @@ namespace Learn.Archives.Core.Model
[SugarColumn(Length = 6)]
public int Year { get; set; }
/// <summary>
/// 年级名称
/// </summary>
[SugarColumn(IsIgnore =true)]
public string Name => GradeHelper.GetGrade(Level.ToString(), Year);
/// <summary>
/// 学校名称
/// </summary>
[SugarColumn(Length = 12)]