我正在编写一个 Asp.Net 核心应用程序,并且在获得导航访问权限之前,我让用户通过 Google 进行身份验证。当用户导航到我的“WorldBuilder”区域并点击 WorldController 时,BaseController.User 对象为空。这是迄今为止我设置的唯一区域/控制器。
用户在登录时有效,因为我可以迭代与用户关联的声明。我正在使用默认的 Asp.Net 核心谷歌身份验证,因此用户只会在登录页面上收到一个按钮,指示他们向谷歌进行身份验证。
[Authorize]
[Area("WorldBuilder")]
public class WorldController : Controller
{
private string _userId;
private IWorldService _worldService;
private IUserRepository _userRepository;
public WorldController(IWorldService worldService, IUserRepository userRepository)
{
if (User != null) /* The user object is found to be null here. */
{
var userIdNameClaim = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
if (userIdNameClaim != null)
{
_userId = userIdNameClaim.Value;
}
}
_userRepository = userRepository;
_worldService = worldService;
}
Run Code Online (Sandbox Code Playgroud)
我希望由于这些区域是同一应用程序的一部分,因此经过身份验证的用户信息将在区域之间传递,但情况似乎并非如此。BaseController.User 对象为 null
我在一个区域托管了一个 API 控制器。但是,路由似乎不起作用,因为我的 ajax 调用在尝试点击控制器操作时不断返回 404。控制器构造函数中的断点永远不会被击中。
[Area("WorldBuilder")]
[Route("api/[controller]")]
[ApiController]
public class WorldApiController : ControllerBase
{
IWorldService _worldService;
IUserRepository _userRepository;
public WorldApiController(IWorldService worldService, IUserRepository userRepository)
{
_worldService = worldService;
_userRepository = userRepository;
}
[HttpGet]
public ActionResult<WorldIndexViewModel> RegionSetSearch()
{
string searchTerm = null;
var userId = User.GetUserId();
WorldIndexViewModel model = new WorldIndexViewModel();
IEnumerable<UserModel> users = _userRepository.GetUsers();
UserModel defaultUser = new UserModel(new Microsoft.AspNetCore.Identity.IdentityUser("UNKNOWN"), new List<Claim>());
model.OwnedRegionSets = _worldService.GetOwnedRegionSets(userId, searchTerm);
var editableRegionSets = _worldService.GetEditableRegionSets(userId, searchTerm);
if (editableRegionSets != null)
{
model.EditableRegionSets = editableRegionSets.GroupBy(rs => …Run Code Online (Sandbox Code Playgroud)