简体   繁体   English

ASP .NET Core 3.1 API 重定向到 prod 环境中的登录页面

[英]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.

相关问题 带有 Identity Core 和 Asp.net Core 3.1 的多重登录页面 - Multiple Login Page with Identity Core and Asp.net Core 3.1 Asp.Net Core 3.1 Cookie 身份验证循环到登录页面 - Asp.Net Core 3.1 Cookie Authentication loop to login page ASP.NET Core 3.1 [Authorize] 属性重定向到登录,即使是登录用户 - ASP.NET Core 3.1 [Authorize] attribute redirects to login even for logged in user Asp.net core 3.1 with Razor 页面重定向到索引页面而不是预期页面 - Asp.net core 3.1 with Razor Pages redirects to the Index page instead of the intended page 在 ASP .NET Core 3.1 中使用 Linkedin 注册/登录 - Register/Login with Linkedin in ASP .NET Core 3.1 asp .net core-3.1登录使用windows身份验证登录页面而不是浏览器弹出窗口 - asp .net core-3.1 login using windows authentication with login page instead of browser popup ASP.NET Core 3.1 中的默认页面 - Default page in ASP.NET Core 3.1 ASP.NET Core 3.1 API Ocelot JWT 在登录时返回 401 - ASP.NET Core 3.1 API Ocelot JWT Returns 401 on Login 在 ASP.NET Core 3.1 中处理多环境的 CORS 策略 - Handling CORS policy for multiple environment in ASP.NET Core 3.1 成功登录后,asp.net core 2.2重定向到登录 - asp.net core 2.2 redirects to login after successful sign in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM