【问题标题】:Asp.NET Core controller action returns 404 when publishedAsp.NET Core 控制器操作在发布时返回 404
【发布时间】:2018-03-21 04:21:05
【问题描述】:

我有一个使用 MVC 的 ASP.NET Core 应用程序,我最近向它添加了身份验证。在本地测试时运行良好,但在我发布应用并将其安装到另一台服务器后,登录方法总是返回 404 错误。

奇怪的是,只有登录方法有这个问题 - 如果我打开 Postman 并向其他 API 方法之一发出请求,它将返回 401 Unauthorized(正如预期的那样,因为我没有登录)。所以我知道我有正确的 URL,但它仍然返回 404。

Login 方法的写法和其他方法一模一样,我能看到的唯一区别是它的属性中有 [AllowAnonymous] 而不是 [Authorize]。

我注意到的另一个奇怪之处是它需要很长时间(约 30 秒)才能返回 404 错误。对其他控制器的请求没有相同的延迟。

这些都没有任何意义——为什么当同一个控制器上的另一个方法被正确路由时,该方法会失败?为什么会出现 404 错误?为什么它可以在我的本地机器上运行?

登录控制器:

[Produces("application/json")]
[Route("api/[controller]")]
public class LoginController : Controller
{
    private readonly ILoginService _service;
    private readonly CustomTokenOptions _tokenOptions;
    private readonly IMemoryCache _memoryCache;

    public LoginController(ILoginService service, IMemoryCache memoryCache, IConfiguration configuration)
    {
        _service = service;
        _tokenOptions = new CustomTokenOptions();
        configuration.GetSection("TokenAuthentication").Bind(_tokenOptions);
        _memoryCache = memoryCache;
    }
    [AllowAnonymous]
    [HttpPost("[action]")]
    public IActionResult GetToken()
    {
        //Returns a JWT token
        //This method returns a 404 Not Found
    }

    [Authorize(Policy="AllowedGroups")]
    [HttpPost("[action]")]
    public IActionResult Logout()
    {
        //This method works
    }
}

客户端代码:

var basePath = $("base").first().attr("href");

function login() {
  $("#btnLogin").attr("disabled", true);
  username = $("#username").val();
  password = $("#password").val();
  request = $.ajax({
    url: basePath + "api/Login/GetToken",
    async: true,
    type: "POST",
    headers: {
      username: username,
      password: password
    }
  });

function logout() {
  request = $.ajax({
    url: basePath + "api/Login/Logout",
    async: true,
    type: "POST",
    headers: {
      "Authorization": "Bearer " + self.token()
    }
  }).then(function () {
    self.loggedIn(false);
    self.token("");
  });
}

我为调试目的尝试过的其他事情:

  • 删除 GetToken() 方法的内容并将其替换为 return StatusCode(200); 这意味着 GetToken 方法中没有任何内容会导致错误。

  • 注释掉我代码中的UseAuthentication 和Authorize 语句。关闭身份验证后仍然会发生错误。

  • 将 GetToken 方法重命名为其他名称。

【问题讨论】:

  • 您是否也使用邮递员测试了 GetToken url?那是否也返回了404?此外,console.log 记录“basePath”变量或在服务器上运行时检查开发人员工具中的请求 url,以确保它到达您期望的 URL。
  • 您能否在LoginController 中发布其他方法和属性?此外,在控制器级别移动 [Authorize(Policy="AllowedGroups")] 并允许您认为合适的匿名用户也是一个好主意。
  • GetToken 在 Postman 中也返回 404,开发人员工具显示它正在访问我期望的 URL(主机名/AppName/api/Login/GetToken)。这是 LoginController 中仅有的两个方法,但我会添加属性。
  • 将 [Authorize] 移动到控制器级别并没有改变。
  • 30 秒听起来像是默认的 SQL Server 连接限制。您的身份提供者可能会遇到问题,尝试无效的 sql 连接、超时并返回 404 而不是 500。

标签: c# authentication asp.net-core url-routing


【解决方案1】:

由@WictorZychla 回答。构造函数中的 LoginService 失败,因此错误发生在控制器操作之前。

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多