【问题标题】:ASP.NET Core 2.2 Authentication not working after deploying the app - Nginx with mutliple apps部署应用程序后 ASP.NET Core 2.2 身份验证不起作用 - 具有多个应用程序的 Nginx
【发布时间】:2020-07-27 05:06:11
【问题描述】:

我正在尝试使用 Nginx 将我的应用程序部署到我们的内部服务器。这是一个 ASP.NET Core 2.2 Razor Pages 站点。我被要求包含一些用于记录目的的身份验证。最终一切都在我的电脑上运行良好。我使用此站点添加基于 cookie 的身份验证: https://www.mikesdotnetting.com/article/335/simple-authentication-in-razor-pages-without-a-database

我做了一些修改以在OnPost() 方法中处理更多用户。虽然我认为这不是问题。

值得一提的是,这不是服务器上运行的唯一 .net 核心应用程序。设置与此类似:

app1: our.domain.com

app2:our.domain.com/app2(这是我遇到的问题)

一切正常,除了登录。当我尝试登录时,如果密码和用户名正确,它将被重定向到正确的页面,但是似乎没有身份或之后找不到它。

在我第一次尝试时,我在 kestrel 服务日志中发现了以下错误:

fail: Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery[7]

我可以使用services.AddDataProtection() 使其消失,但是问题仍然存在,我被重定向 - 或者如果登录尝试不正确,则会收到错误消息 - 但仍然无法访问授权文件夹,例如HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value 返回 null 或空。

我做了一个快速测试,并添加了相同的登录页面和授权文件夹,以及其他应用程序的其他依赖项。它在那里工作。我什至没有在 startup.cs 中包含services.AddDataProtection()。登录工作完美。虽然它使用的是 .net core 2.1。

所以它可能与生根有关?或者我不知道。我完全迷路了。我不是一个全职的开发者,更像是一个业余爱好者,我现在完全被困住了。也许我在 startup.cs 中搞砸了一些东西?或者我应该添加其他东西?还是与 cookie 处理有关?我确实做了很多搜索,到目前为止没有运气。

这是我的 startup.cs 的相关部分:

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddEntityFrameworkNpgsql()
                .AddDbContext<Models.UserAccessDbContext>(options =>
                    options.UseNpgsql(Configuration.GetConnectionString("appConnection")))
                .BuildServiceProvider();


            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            // https://hanselman.com/blog/DealingWithApplicationBaseURLsAndRazorLinkGenerationWhileHostingASPNETWebAppsBehindReverseProxies.aspx
            services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.All;
                options.AllowedHosts = Configuration.GetValue<string>("AllowedHosts")?.Split(';').ToList<string>();
            });


            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(cookieOptions =>
            {
                cookieOptions.LoginPath = "/";
            });

            services.AddMvc().AddRazorPagesOptions(options =>
            {
                options.Conventions.AuthorizeFolder("/admin");
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


            services.AddDataProtection()
                .SetApplicationName("app")
                .PersistKeysToFileSystem(new DirectoryInfo(@"/var/dpkeys/"));


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();

                app.Use((context, next) =>
                {
                    context.Request.PathBase = new PathString("/app");
                    return next.Invoke();
                });
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();


            app.UseAuthentication();

            app.UseMvc();
        }

更新: 一个小细节。 cookie 已创建,我可以在 Chrome 的检查器中看到它。但是网站/应用程序不认为我是经过身份验证的用户。

【问题讨论】:

    标签: authentication nginx razor-pages asp.net-core-2.2


    【解决方案1】:

    似乎我的 cookie 设置有问题。两个页面之间还有一个区别,如果它使用font-awesome,这意味着我的网站上有一些外部cookie。作为一个疯狂的猜测,首先我设置了

    options.MinimumSameSitePolicy = SameSiteMode.Lax;
    

    因为其他 cookie 具有该模式。

    就这样,它也开始在托管服务器上运行!现在应用程序可以识别登录后创建的 cookie。

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 2020-06-20
      • 2019-10-30
      • 2019-11-15
      • 2020-03-21
      • 2019-06-30
      • 1970-01-01
      • 2016-06-14
      • 2022-01-03
      相关资源
      最近更新 更多