小编cha*_*986的帖子

为什么在区域控制器加载时我的 BaseController.User 值 == null?

我正在编写一个 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

c# asp.net-core-mvc asp.net-core

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

Asp.Net 核心区域路由到 Api 控制器不起作用

我在一个区域托管了一个 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)

asp.net-mvc-areas asp.net-core-mvc asp.net-core-2.0

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