【问题标题】:ASP.NET CORE Identity DataProtectionTokenProviderOptions Password reset token is invalidASP.NET CORE Identity DataProtectionTokenProviderOptions 密码重置令牌无效
【发布时间】:2020-12-15 00:39:33
【问题描述】:

对于我的应用程序,我实现了 ASP.NET Core Identity。我使用 GeneratePasswordResetTokenAsync() 生成密码重置令牌。如果我在 1 天内重置密码,它工作正常。 1天后它将不再起作用。 i 收到“无效令牌”消息。我已经将 DataProtectionTokenProviderOptions 中的 TokenLifeSpan 设置为 Timespan.FromDays(3),但这似乎仍然不起作用。

但是,如果我在其中更改密码,它确实有效。有人可以帮助我,因为我真的被这个问题困住了。谢谢。

public void ConfigureServices(IServiceCollection services)
    {
        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;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        var connection = Configuration.GetConnectionString("DefaultConnection");
        services.AddDbContext<DUMMY_DBContext>(options => options.UseSqlServer(connection));
        services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<DUMMY_DBContext>().AddDefaultTokenProviders();
        services.Configure<DataProtectionTokenProviderOptions>(o => o.TokenLifespan = TimeSpan.FromDays(3));
    }

【问题讨论】:

  • 你能把你的startup.cs代码的sn-p放在这里。您可以在上面edit您的问题。
  • @PriyankPanchal ??
  • 我看不出你所做的有什么问题。长镜头,也许试着写你的最后一行像services.Configure&lt;DataProtectionTokenProviderOptions&gt;(o =&gt; { o.TokenLifespan = TimeSpan.FromDays(3); }); 注意我刚刚添加了花括号。
  • @PriyankPanchal 不起作用..

标签: asp.net-mvc visual-studio asp.net-core asp.net-identity identity


【解决方案1】:

您忘记了一些事情:为自定义令牌添加名称,设置要用于密码重置的令牌并将其添加到身份。

您的代码应如下所示:

      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;
            });
            
            //Configure must be before you register the service 
            services.Configure<DataProtectionTokenProviderOptions>(o => { 
                o.TokenLifespan = TimeSpan.FromDays(3);
                o.Name = "3DaysToken";
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            var connection = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext<DUMMY_DBContext>(options => options.UseSqlServer(connection));
            services.AddIdentity<IdentityUser, IdentityRole>(options => {
                options.Tokens.PasswordResetTokenProvider = "3DaysToken"; //Set password token provider
            })
                .AddEntityFrameworkStores<DUMMY_DBContext>().AddDefaultTokenProviders()
                .AddTokenProvider<
                    DataProtectorTokenProvider<IdentityUser>>(
                    "3DaysToken"); //Added extended token provider
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2015-04-28
    • 2021-01-23
    • 2015-05-29
    • 2020-09-01
    • 2017-08-12
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多