如何使SignInAsync在此处正常工作



有人能告诉我如何修复这个错误吗?

HttpContext不包含SignInAsync的定义。

在我用教程制作的另一个项目中,它工作得很好。使用包:Microsoft.AspNetCore.Http;

public async Task<ActionResult<User>> LoginUser(LoginDto dto)
{
User loggedInUser = await _context.Users.Where(u => u.Email == dto.Email && u.PasswordHash == HashDtoPassword(dto.Password).PasswordHash).FirstOrDefaultAsync();
if (loggedInUser != null)
{
//create a claim
var claim = new Claim(ClaimTypes.Name, loggedInUser.Email);
//create claimsIdentity
var claimsIdentity = new ClaimsIdentity(new[] { claim }, "serverAuth");
//create claimsPrincipal
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
//Sign In User
//error down here 
await HttpContext.SignInAsync(claimsPrincipal);
}
return await Task.FromResult(loggedInUser);
}

SigninAsyncMicrosoft.AspNetCore.Authentication命名空间中类HttpContext的扩展方法。下面是一个基于新的Web API项目的工作示例。您可能需要添加using Microsoft.AspNetCore.Authentication;

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication3.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<ActionResult> GetAsync()
{
await HttpContext.SignInAsync("scheme", new System.Security.Claims.ClaimsPrincipal());
return Ok();
}
}
}

相关内容

  • 没有找到相关文章