简体   繁体   English

从核心 2.1 升级到核心 3.1 时如何解决此错误 OptionsValidationException: Cookie.Expiration is ignored, use ExpireTimeSpan instead

[英]how to solve this error when upgrade from core 2.1 to core 3.1 OptionsValidationException: Cookie.Expiration is ignored, use ExpireTimeSpan instead

I have upgraded my asp.net core 2.1 application to asp.net core 3.1, but when I run the project I got this error OptionsValidationException: Cookie. Expiration is ignored, use ExpireTimeSpan instead.我已将我的 asp.net 核心 2.1 应用程序升级到 asp.net 核心 3.1,但是当我运行该项目时,我收到此错误OptionsValidationException: Cookie. Expiration is ignored, use ExpireTimeSpan instead. OptionsValidationException: Cookie. Expiration is ignored, use ExpireTimeSpan instead. the first display of my application show this error我的应用程序的第一个显示显示此错误

here is my startup code asp.net core 3.1, that I have upgraded from .net core 2.1,这是我的启动代码 asp.net 核心 3.1,我已经从 .net 核心 2.1 升级,

  public class Startup
{
    public IConfiguration Configuration { get; }
    public IWebHostEnvironment Environment { get; }
    public Startup(IConfiguration configuration, IWebHostEnvironment environment)
    {
        Configuration = configuration;
        Environment = environment;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        var connection = Configuration.GetConnectionString("DentalDBconnection");
        services.AddDbContext<HoshmandDBContext>(option => option.UseSqlServer(connection));
        services.AddAuthentication(option =>
        {
            option.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            option.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            option.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie(options =>
        {
            options.LoginPath = "/Logins/UserLogin/";
            options.AccessDeniedPath = "/AccessDenied";
            options.Cookie.Expiration = new TimeSpan(10, 00, 00);
        });

        services.AddDistributedMemoryCache();
        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromHours(2);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;

        });

        services.ConfigureApplicationCookie(option =>
        {
            option.ExpireTimeSpan = TimeSpan.FromMinutes(540);
        });

        services.AddAuthorization(options =>
        {
            options.AddPolicy("HasAccess", policy => policy.AddRequirements(new HasAccessRequirment()));
        });
        // services.AddTransient<IFingerPrint, FingerPrint>();
        services.AddTransient<IAuthorizationHandler, HasAccessHandler>();
        services.AddTransient<IMvcControllerDiscovery, MvcControllerDiscovery>();
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        if (Environment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        var cachePeriod = Environment.IsDevelopment() ? "600" : "604800";
        app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = ctx =>
            {
                ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
            }
        });
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseCors();
        app.UseAuthentication();
        app.UseCookiePolicy();
        app.UseSession();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(name: "default", pattern: "{controller=UserProfiles}/{action=Index}/{id?}");
        });

please help me, what else I need to change when upgrating from asp.net core 2.1 to asp.net core 3.1 to not got this error Thank you请帮助我,从 asp.net 核心 2.1 升级到 asp.net 核心 3.1 时我还需要更改什么才能没有出现此错误谢谢

New Section Code Added添加了新的部分代码

I think the issue is somewhere else by default in my startup code user redirects to this controller, from EndPoint我认为默认情况下,问题出在我的启动代码中的其他地方,用户从 EndPoint 重定向到这个 controller

endpoints.MapControllerRoute(name: "default", pattern: "{controller=UserProfiles}/{action=Index}/{id?}");

in UserProfile Controller there is, a check if a user is not login, this function redirects to the user's login page在 UserProfile Controller 中,检查用户是否未登录,此 function 重定向到用户的登录页面

public async Task<IActionResult> Index(bool? passIsChanged = null) // userId
    {
        if (passIsChanged != null)
        {
            ViewBag.isSuccessed = passIsChanged;
        }
        int id = Convert.ToInt32(User.Identity.Name);
        var user = _context.UserAccountTbs.FirstOrDefault(a => a.UserId == id);
        if (user == null)
        {
            return RedirectToAction("UserLogin", "Logins");
        }

From UserProfile when GO to the login page this Error Happens从 GO 时的 UserProfile 到登录页面出现此错误

Use options.ExpireTimeSpan instead of options.Cookie.Expiration (which, yes, is ignored).使用options.ExpireTimeSpan而不是options.Cookie.Expiration (是的,它被忽略了)。

services.AddAuthentication(...)
   .AddCookie(options =>
   {
       options.LoginPath = "/Logins/UserLogin/";
       options.AccessDeniedPath = "/AccessDenied";
       options.ExpireTimeSpan = new TimeSpan(10, 00, 00);
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将启动代码从 asp.net core 2.1 转换为 Startup Code asp.net Core 3.1 - How to Convert Startup Code from asp.net core 2.1 to Startup Code asp.net Core 3.1 从 .NET Core 2.1 迁移到 v3.1 后出现“被 CORS 阻止”错误 - "Blocked by CORS" error after migrating from .NET Core 2.1 to v3.1 从.NET Core 2.1迁移到.NET Core 3.1后,publish出现问题 - After migrating from .NET Core 2.1 to .NET Core 3.1, publish has a problem 基于 IPAddress 和客户端设备在 asp.net 核心 3.1 身份中设置 Cookie Expiration 时间跨度 - Set Cookie Expiration time span based on IPAddress and client device in asp.net core 3.1 identity 如何在没有 EF Core 的情况下在 ASP .NET Core 3.1 中使用 Identity? - How to use Identity in ASP .NET Core 3.1 without EF Core? 如何从 a.Net Core 3.1 进程启动 a.Net Core 3.1 进程? - How to start a .Net Core 3.1 process from a .Net Core 3.1 process? 如何使用从 .Net Core 3.1 中的 web.config 读取的 DLL? - how can I use a DLL that reades from web.config in .Net Core 3.1? Asp.net core 3.1 后端无法从 cookie 中获取身份 - Asp.net core 3.1 backend does not get the Identity from cookie 如何制作一种方法来验证 cookie 身份验证? ASP.NET 核心 3.1 - how to made a method to validate a cookie authentication? asp.net core 3.1 如何从 .NET Core 3.1 中的 JSON object 反序列化浮点数据类型 - How to Deserialize float datatype from JSON object in .NET Core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM