知道为什么我会出现这个错误吗? 错误信息 --> "IServiceCollection不包含AddDefaultIdentity的定义"
我正在从.NET Core v1.1迁移到v3.1。
public class Program
{
public async static void Main(string[] args)
{
await Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseKestrel();
webBuilder.UseAzureAppServices();
webBuilder.UseStartup<Startup>();
})
.Build()
.RunAsync();
}
}
public class Startup
{
public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
{
Configuration = configuration;
HostEnvironment = hostEnvironment;
}
public IConfiguration Configuration { get; }
protected IApplicationBuilder ApplicationBuilder { get; private set; }
public IHostEnvironment HostEnvironment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// services.AddRazorPages();
services.AddDefaultIdentity<ApplicationUser>() // "ApplicationUser" is named incorrectly, it should be "IdentityUser" instead, as per Microsoft documentation.
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationContext, Guid>() // FYI - AddEntityFrameworkStores() deal with role that derives from IdentityRole, as per documentation.
//.AddDefaultUI()
.AddDefaultTokenProviders();
// [ Old version #1 - replacement ]
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = new PathString("/Home/Index");
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(this.Configuration.GetValue<int?>("Authentication:SlidingExpirationTime").Value);
options.AccessDeniedPath = new PathString("/Home/AccessDenied");
});
// [ Old version #2 - replacement ]
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.Password.RequiredLength = 7;
});
services.AddMvc();
services.AddSession();
//services.Configure<AuthorizationOptions>(options =>
//{
//});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
// Config Exception.
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Home/ErrorPage.html");
app.UseStaticFiles(); // Note, we are not authenticating for static files if this is before them
app.UseSession();
app.UseAuthentication();
// MVC.
// app.UseMvc(routes => routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"));
}
}
public class ApplicationUser : IdentityUser<Guid>, IUser
{
}
public interface IUser
{
}
public class ApplicationContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
public ApplicationContext(DbContextOptions<ApplicationContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureStorage" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.3" />
<PackgaeReference Include="Microsoft.Extensions.Hosting" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Microsoft.AspNetCore.AzureAppServicesIntegration" Version="1.0.2" />
</ItemGroup>
好的,通过添加 "Microsoft.AspNetCore.Identity.UI "的NuGet包来帮助我。 现在我又遇到了另一个错误。 :- 我无法理解这个错误。
services.AddDefaultIdentity<ApplicationUser>()
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationContext, Guid>()
.AddDefaultTokenProviders();
第一行的错误现在已经消失了。 但是现在新的错误出现在第三行,"AddEntityFrameworkStore()"。 错误信息是--> "IdentityBuilder "不包含 "AddEntityFrameworkStores "的定义& 找不到可访问的扩展方法 "AddEntityFrameworkStores "接受第一个参数类型为 "IdentityBuilder "的方法(你是在使用指令还是在使用汇编引用?
我甚至不知道这个 "AddEntityFrameworkStores "是从哪个NuGet包里出来的&从版本1到3.1有什么变化。
你需要在程序中添加一个对 Microsoft.AspNetCore.Identity.UI nuget包 为了使用 AddDefaultIdentity
. 但如果您不想迁移到Identity Razor类库,我认为您仍然可以使用 .AddIdentity<ApplicationUser, IdentityRole>()
的核心3.1中。 如果你确实想迁移到RCL,2.0到2.1的迁移文档可能是一个很好的起点。https:/docs.microsoft.comen-usaspnetcoremigration20_21?view=aspnetcore-3.1#changes-to-authentication-code(认证代码变更)
--- 编辑 ---
我已经把几个网站从1.1迁移到3.1,我发现最简单的方法是这样做的。
要让它正常工作还需要很多时间,否则你就需要从1.x到2.0的迁移文档开始,一直到3.0到3.1的文档。