小编Nic*_*ick的帖子

ASP.NET Core 实体更改历史

我有很多这样的控制器:

public class EntityController : Controller
{
    private readonly IEntityRepository _entity;

    public EntityController(IEntityRepository entity)
    {
        _entity = entity;
    }

    [Authorize]
    [HttpPut("{id}")]
    public async ValueTask<IActionResult> Put(int id, [FromBody] Entity entity)
    {
        if (entity == null || entity.Id != id) return BadRequest();
        var updated = await _entity.Update(entity);
        if (updated == null) return NotFound();
        return Ok(updated);
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要实现实体编辑(审计)历史。

而且,由于该方法被标记为[Authorize],我需要记录它是由哪个用户编辑的。我正在看Audit.NET,但我没有找到方法来做到这一点。

audit-logging entity-framework-core asp.net-core audit.net

2
推荐指数
1
解决办法
1785
查看次数

ASP.NET核心标识中的角色声明的JWT身份验证

我如何通过声明对用户进行身份验证,其中包含用户角色?

Startup.cs:

services.AddAuthorization(options => {
                options.AddPolicy("CanEdit", policy => policy.RequireClaim("CanEdit"));    
});
Run Code Online (Sandbox Code Playgroud)

在登录控制器中我有:

    private async ValueTask<JwtSecurityToken> GetJwtSecurityToken(ApplicationUser user){
        //var totalClaims = new List<Claim>();
        //var userRoles = await _userManager.GetRolesAsync(user);
        //foreach (var role in userRoles) {
        //    var roleClaims = await _roleManager.GetClaimsAsync(await _roleManager.Roles.SingleAsync(r => r.Name.Equals(role)));
        //    totalClaims.AddRange(roleClaims);
        //}
        var claims = new List<Claim>
        {
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
            new Claim(JwtRegisteredClaimNames.Email, user.Email)
        };
        return new JwtSecurityToken(
            _configuration["Token:Issuer"],
            _configuration["Token:Audience"],
            //totalClaims,
            claims
            expires: DateTime.UtcNow.AddHours(12),
            signingCredentials: new SigningCredentials(
                new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Token:Key"])),
                SecurityAlgorithms.HmacSha256)
        );
    }
Run Code Online (Sandbox Code Playgroud)

方法 …

c# jwt asp.net-identity asp.net-core asp.net-core-webapi

1
推荐指数
1
解决办法
4356
查看次数

从内部类继承

public class A {
    public A() {
        System.out.println("A()");
    }

    public class B {
        public B() {
            System.out.println("B()");
        }
    }
}
class Caller extends A.B {
    Caller(A a){
        a.super();
    }
}


public class Main {
    public static void main(String[] args) {
        Caller as= new Caller(new A());
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我们需要a.super()在课堂上调用扩展内部类?它在做什么?

没有a.super()程序不想编译.

Error:(48, 20) java: an enclosing instance that contains A.B is required
Run Code Online (Sandbox Code Playgroud)

java oop class

-3
推荐指数
1
解决办法
106
查看次数