我有很多这样的控制器:
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,但我没有找到方法来做到这一点。
我如何通过声明对用户进行身份验证,其中包含用户角色?
在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)
方法 …
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)