Asp.net core IServiceCollection不包含AddDefaultIdentity的定义
知道我为什么会出现此错误吗?错误消息-->“IServiceCollection不包含AddDefaultIdentity的定义” 我正在从.NETCoreV1.1迁移到v3.1Asp.net core IServiceCollection不包含AddDefaultIdentity的定义,asp.net-core,.net-core,asp.net-core-identity,Asp.net Core,.net Core,Asp.net Core Identity,知道我为什么会出现此错误吗?错误消息-->“IServiceCollection不包含AddDefaultIdentity的定义” 我正在从.NETCoreV1.1迁移到v3.1 public class Program { public async static void Main(string[] args) { await Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(we
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>
公共类程序
{
公共异步静态void Main(字符串[]args)
{
等待主机。CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>{
UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseKestrel();
webBuilder.UseAzureAppServices();
webBuilder.UseStartup();
})
.Build()
.RunAsync();
}
}
公营创业
{
公共启动(I配置配置、I hostEnvironment hostEnvironment)
{
配置=配置;
招待环境=招待环境;
}
公共IConfiguration配置{get;}
受保护的IAApplicationBuilder ApplicationBuilder{get;private set;}
公共IHostEnvironment HostEnvironment{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
//services.AddRazorPages();
services.AddDefaultIdentity()/“ApplicationUser”的名称不正确,根据Microsoft文档,应改为“IdentityUser”。
.AddRoles()
.AddEntityFrameworkStores()//仅供参考-AddEntityFrameworkStores()根据文档处理从IdentityRole派生的角色。
//.AddDefaultUI()
.AddDefaultTokenProviders();
//[旧版本#1-更换]
services.configureApplicationOK(选项=>
{
options.LoginPath=新路径字符串(“/Home/Index”);
options.SlidingExpiration=true;
options.ExpireTimeSpan=TimeSpan.FromMinutes(this.Configuration.GetValue(“身份验证:SlidingExpirationTime”).Value);
options.AccessDeniedPath=新路径字符串(“/Home/AccessDenied”);
});
//[旧版本#2-更换]
配置(选项=>
{
options.Password.RequireUppercase=false;
options.Password.RequireLowercase=false;
options.Password.RequireNonAlphanumeric=false;
options.Password.RequireDigit=false;
options.Password.RequiredLength=7;
});
services.AddMvc();
services.AddSession();
//配置(选项=>
//{
//});
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
});
//配置异常。
if(env.IsDevelopment())
app.UseDeveloperExceptionPage();
其他的
app.UseExceptionHandler(“/Home/ErrorPage.html”);
app.UseStaticFiles();//注意,如果在静态文件之前,我们不会对其进行身份验证
app.UseSession();
app.UseAuthentication();
//MVC。
//app.UseMvc(routes=>routes.MapRoute(“默认值”,“{controller=Home}/{action=Index}/{id?}”);
}
}
公共类应用程序用户:IdentityUser,IUser
{
}
公共接口IUser
{
}
公共类应用程序上下文:IdentityDbContext
{
公共应用程序上下文(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
}
}
netcoreapp3.1
--已编辑-新更新如下-------------------------------------------------------
好的,通过添加“Microsoft.AspNetCore.Identity.UI”NuGet包来帮助您。现在我遇到了另一个错误。:-/我搞不懂这个
services.AddDefaultIdentity<ApplicationUser>()
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationContext, Guid>()
.AddDefaultTokenProviders();
services.AddDefaultIdentity()
.AddRoles()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
第一行的错误现在消失了。但现在第三行出现了新错误,“AddEntityFrameworkStore()”。错误消息是-->“IdentityBuilder”不包含“AddEntityFrameworkStores”的定义&找不到可访问的扩展方法“AddEntityFrameworkStores”,该扩展方法接受类型为“IdentityBuilder”的第一个参数(您是否误判了using指令或程序集引用?)
甚至不确定此“AddEntityFrameworkStores”来自哪个NuGet软件包,也不知道它从版本1更改为3.1的原因。您需要添加对的引用才能使用
AddDefaultIdentity
。但是如果您不想迁移到Identity Razor类库,我认为您仍然可以使用.AddIdentity
---编辑---
我已经将一些站点从1.1迁移到3.1,我发现最简单的方法是:
将整个解决方案移动到备份文件夹(确保将源代码管理文件保留在原来的位置)
在原始位置创建一个与目标3.1名称完全相同的新应用程序。我在VS2019中使用“Web应用程序(模型视图控制器)”模板,并将身份验证更改为“个人用户帐户”
将其提交给源代码管理,以便查看所做的更改
将所有页面、视图、控制器和其他代码复制到新应用程序
添加任何缺少的nuge