[英]ASP .NET Core 3.1 API redirects to login page in prod environment
I created an ASP .NET Core 3.1 website which uses MVC (at least to the best of my understanding).我创建了一个使用 MVC 的 ASP .NET Core 3.1 网站(至少据我所知)。 Apart from the login page I use cookie auth to restrict access.除了登录页面,我还使用 cookie auth 来限制访问。 One of the controllers has got an action decorated with the [HttpPost]
and [AllowAnonymous]
attributes.其中一个控制器有一个用[HttpPost]
和[AllowAnonymous]
属性装饰的动作。 When running the website on my computer everything works just fine, but when I deploy it to my prod server (Server 2016 + IIS) and post to the action using Postman I get redirected to the login page and the HTML of the login page is returned.在我的计算机上运行网站时一切正常,但是当我将其部署到我的产品服务器(服务器 2016 + IIS)并使用 Postman 发布到操作时,我被重定向到登录页面并返回登录页面的 HTML . The rest of the website, login etc works just fine.网站的rest,登录等工作正常。 It's only the API I'm having problems with.这只是我遇到问题的 API。
I also tried creating a brand new controller using the template from Visual Studio, but the result is the same.我还尝试使用 Visual Studio 的模板创建一个全新的 controller,但结果是一样的。 Removing the [Authorize]
attribute from the controller also did not change the situation.从 controller 中删除[Authorize]
属性也没有改变这种情况。
Would anybody know why that might be?有人知道为什么会这样吗? Let me know if there is any information missing you might need.让我知道是否缺少您可能需要的任何信息。
My controller (simplified)我的 controller(简体)
[Authorize]
public class ApplicationController : Controller
{
...
public async Task<IActionResult> Index([FromQuery] string searchTerms, [FromQuery] bool updateDb)
{
...
return View(vm);
}
[AllowAnonymous]
[HttpPost]
public string PostAction([FromBody] object postBody)
{
//Process data (JSON)
return "SomeProcessedDataString";
}
...
}
Cookie auth stuff in startup.cs
startup.cs
中的 Cookie 身份验证内容
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("myCookieScheme")
.AddCookie("myCookieScheme", (CookieAuthenticationOptions config) =>
{
config.Cookie.Name = "myLoginCookie";
config.LoginPath = "/Home/Login";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseAuthentication();
app.UseAuthorization();
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllers();
});
Thank you.谢谢你。
I managed to fix the problem myself.我设法自己解决了这个问题。 Due to file system permission settings on the prod server the POST action was erroring out, which then caused ASP.NET core to redirect to the login page.由于 prod 服务器上的文件系统权限设置,POST 操作出错,然后导致 ASP.NET 内核重定向到登录页面。 Hope this helps somebody else in the future.希望这对将来的其他人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.