feat(api): аутентификация, отключен кастомный BaseSimpleRoleProvider, роли добавляются в ParrAuthenticationHandler

This commit is contained in:
Mikhail Trubnikov
2023-12-04 08:48:13 +10:00
parent dc0c2b340b
commit e0b0297c15
4 changed files with 8 additions and 3 deletions

View File

@@ -46,6 +46,8 @@ namespace PARR.API.Authentication
var claims = new List<Claim> { var claims = new List<Claim> {
new Claim(ClaimTypes.Name, user.UserIp) new Claim(ClaimTypes.Name, user.UserIp)
}; };
// добавляем роли
user.Roles.ForEach(r => claims.Add(new Claim(ClaimTypes.Role, r.Name)));
var claimsIdentity = new ClaimsIdentity(claims, Scheme.Name); var claimsIdentity = new ClaimsIdentity(claims, Scheme.Name);
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

View File

@@ -28,8 +28,8 @@ namespace PARR.API.Controllers.V1
/// Получить мой ip /// Получить мой ip
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
//[Authorize(Roles = ParrRoles.Administrator.Role)] [Authorize(Roles = ParrRoles.Administrator.Role)]
[Authorize] //[Authorize]
[HttpGet(ApiRoutes.Test.GetMyIp)] [HttpGet(ApiRoutes.Test.GetMyIp)]
public IActionResult GetMyIp() public IActionResult GetMyIp()
{ {

View File

@@ -35,7 +35,8 @@ builder.Services.AddDallSettings(builder.Configuration);
// Auth // Auth
builder.Services.AddAuthentication(ParrAuthenticationOptions.DefaultScheme) builder.Services.AddAuthentication(ParrAuthenticationOptions.DefaultScheme)
.AddScheme<ParrAuthenticationOptions, ParrAuthenticationHandler>(ParrAuthenticationOptions.DefaultScheme, opt => { }); .AddScheme<ParrAuthenticationOptions, ParrAuthenticationHandler>(ParrAuthenticationOptions.DefaultScheme, opt => { });
builder.Services.AddSimpleRoleAuthorization<BaseSimpleRoleProvider>(); // не используем, так как проверка ролей остается прежней, а роли подставляем в ParrAuthenticationHandler
//builder.Services.AddSimpleRoleAuthorization<BaseSimpleRoleProvider>();
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();

View File

@@ -13,6 +13,8 @@ namespace PARR.API.RoleProvider
public async Task<ICollection<string>> GetUserRolesAsync(string ipClient) public async Task<ICollection<string>> GetUserRolesAsync(string ipClient)
{ {
// это всё не используем, так как проверка ролей остается прежней, а роли подставляем в ParrAuthenticationHandler
// Авторизация - проверка на разрешения // Авторизация - проверка на разрешения
// В данном случае, получаем все разрешения (роли) // В данном случае, получаем все разрешения (роли)