AspNetCore身份2.0和2.1的差异

时间:2019-03-01 04:48:09

标签: c# asp.net-core asp.net-core-2.1 asp.net-2.0 asp.net-core-identity

  • AddIdentity和AddDefaultIdentity有什么区别

从AspNetCore身份2.0启动时配置服务的方法

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        services.AddMvc();
    }

从AspNetCore身份2.1启动时配置服务的方法

 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.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

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

1 个答案:

答案 0 :(得分:0)

AddDefaultIdentity是ASP.NET Core 2.1中引入的。调用AddDefaultIdentity类似于调用以下代码:

  1. AddIdentity

  2. AddDefaultUI

  3. AddDefaultTokenProviders

请参阅https://github.com/aspnet/Docs/issues/8434