我在核心中使用Identity。
启动时。
services.AddSingleton<ICurrentUserService, CurrentUserService>();
services.AddHttpContextAccessor();
和账户管理员
[ApiController]
[Route("api/[controller]")]
public class AccountController : Controller
{
private readonly IApplicationUserManager _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
public AccountController
(IApplicationUserManager userManager,
SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
//_roleManager = roleManager;
_signInManager = signInManager;
}
[HttpPost("Login")]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model)
{
try
{
await _signInManager.SignOutAsync();
var result = await _signInManager.PasswordSignInAsync(model.email, model.password, true, false);
if (result.Succeeded)
{
return Json(new { success = true});
}
var message = string.Join("; ", ModelState.Values
.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage));
return Json(new
{
success = false,
Message = message
});
}
catch (Exception e)
{
return Json(new
{
success = false,
Message = e.Message
});
}
}
[HttpPost("logout")]
public IActionResult LogOut()
{
try
{
_signInManager.SignOutAsync();
return Ok(new { success = true });
}
catch (Exception e)
{
return Ok(new { success = false, Message = e.Message });
}
}
}
}
和在CurrentUserService 中
public class CurrentUserService : ICurrentUserService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public CurrentUserService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public Guid? UserId => Guid.Parse(_httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier) ?? string.Empty);
public string FullName => _httpContextAccessor.HttpContext?.User?.FindFirstValue("FullName") != null
? _httpContextAccessor.HttpContext?.User?.FindFirstValue("FullName")
: _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Name);
public string Avatar => _httpContextAccessor.HttpContext?.User?.FindFirstValue("Avatar") != null
? _httpContextAccessor.HttpContext?.User?.FindFirstValue("Avatar")
: "/Themes/img/no_avatar.png";
}
但CCD_ 1始终为空。
更新:
我将此添加到startup
。没事。
app.UseAuthorization();
app.UseAuthentication();
如果您想访问DbContext类中的IHttpContextAccessor
及其重写的SavingChanges方法,请不要这样做!相反,创建一个SaveChangesInterceptor
,然后像往常一样注入您的服务:
public class AuditableEntitiesInterceptor : SaveChangesInterceptor
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<AuditableEntitiesInterceptor> _logger;
public AuditableEntitiesInterceptor(
IHttpContextAccessor httpContextAccessor,
ILogger<AuditableEntitiesInterceptor> logger)
{
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
然后你应该注册你的新拦截器:
services.AddSingleton<AuditableEntitiesInterceptor>();
最后以这种方式将其添加/引入EF Core:
services.AddDbContextPool<ApplicationDbContext>((serviceProvider, optionsBuilder) =>
optionsBuilder.AddInterceptors(serviceProvider.GetRequiredService<AuditableEntitiesInterceptor>());