无法在CookieAuthenticationOptions中设置AuthenticationType

问题描述 投票:0回答:1

我正在尝试实现this用户提出的解决方案,以防止在我的应用程序中进行多次登录。

实际上我在我的Startup类中声明,特别是在Configure方法中这段代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{

});

问题是,当我输入:AuthenticationType我没有显示任何内容,因为CookieAuthenticationOptions没有这个属性,这很奇怪,因为在documentation中属性不再存在。

如果我将鼠标悬停在CookieAuthenticationOptions上,我可以看到这个命名空间:Assembly Microsoft.AspNetCore.Authentication.Cookies

PS:我正在使用ASP.NET CORE

c# asp.net asp.net-mvc asp.net-core asp.net-identity
1个回答
1
投票

app.UseCookieAuthentication()已弃用ASP.NET Core 2.X,您应该在app.UseAuthentication()方法中使用Configure,但是您需要在ConfigureServices方法中配置身份验证。

使用NuGet包Microsoft.AspNetCore.Mvc版本2.1.0或更高版本应该像这样配置:

public void ConfigureServices(IServiceCollection services)
{
    // Add the needed services, e.g. services.AddMvc();

    services
        .AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie(options =>
        {
            // Change the options as needed
        });            
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc();
}
© www.soinside.com 2019 - 2024. All rights reserved.