【发布时间】:2019-12-06 18:57:25
【问题描述】:
我有一个现有的ApplicationUser,我想修改登录的 UI 页面。所以这就是我决定执行脚手架操作的原因。
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public List<Student> Students { get; set; }
}
但是当我使用最后一个CLI 命令时:
dotnet aspnet-codegenerator identity --files="Account.Login;"
我无法运行我的网络应用程序,这是它在网络上显示的内容:
然后,这是日志中发生的错误:
未处理的异常:System.InvalidOperationException:Scheme 已经 存在:Identity.Application 在 Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(字符串 名称,Action
1 configureBuilder) at Microsoft.AspNetCore.Authentication.AuthenticationBuilder.<>c__DisplayClass4_02.b__0(AuthenticationOptions o) 在 Microsoft.Extensions.Options.ConfigureNamedOptions1.Configure(String name, TOptions options) at Microsoft.Extensions.Options.OptionsFactory1.Create(字符串名称)
在 Microsoft.Extensions.Options.OptionsManager1.<>c__DisplayClass5_0.<Get>b__0() at System.Lazy1.ViaFactory(LazyThreadSafetyMode 模式)在 System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() 在 Microsoft.Extensions.Options.OptionsCache1.GetOrAdd(String name, Func1 createOptions) 在 Microsoft.Extensions.Options.OptionsManager1.Get(String name) at Microsoft.Extensions.Options.OptionsManager1.get_Value() 在 Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions1 options, IDictionary2 方案) 在 Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions1 options) --- End of stack trace from previous location where exception was thrown --- at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument 参数)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite、ServiceProviderEngineScope 范围)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite,ServiceProviderEngineScope 范围)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument 参数)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.c__DisplayClass1_0.b__0(ServiceProviderEngineScope 范围)在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型 serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型 服务类型)在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型 服务类型)在 Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider 提供者)在 Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider 提供者,类型 instanceType,Object[] 参数)在 Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.c__DisplayClass4_0.b__0(RequestDelegate 下一个)在 Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.StartAsync(CancellationToken 取消令牌)在 Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost 主机, CancellationToken 令牌,字符串 shutdownMessage)在 Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost 主机, CancellationToken 令牌)在 Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost 主机)
在 ICFERApp.Program.Main(String[] args) 中 /Users/idrislutaaya/RiderProjects/ICFERApp/ICFERApp/Program.cs:第 17 行
我看过这个question,但没有帮助。
其实让我吃惊的是,有一个 DBClass 在这个目录 ~/Areas/Identity/Data 中创建了它自己,如下:
public class ICFERAppIdentityDbContext : IdentityDbContext<IdentityUser>
{
public ICFERAppIdentityDbContext(DbContextOptions<ICFERAppIdentityDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
但我这里有一个现有的:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>()
.HasMany(s => s.Students)
.WithOne(u => u.User);
builder.Entity<Student>()
.HasOne(e => e.Education)
.WithOne(s => s.Student);
builder.Entity<Student>()
.HasOne(g => g.Guardian)
.WithOne(s => s.Student);
builder.Entity<Student>()
.HasOne(p => p.Parents)
.WithOne(s => s.Student);
builder.Entity<Student>()
.HasOne(s => s.Siblings)
.WithOne(s => s.Student);
builder.Entity<Siblings>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
builder.Entity<Siblings>()
.HasKey(x => new { x.Id, x.StudentId});
builder.Entity<Education>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
builder.Entity<Education>()
.HasKey(x => new { x.Id, x.StudentId});
builder.Entity<Guardian>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
builder.Entity<Guardian>()
.HasKey(x => new { x.Id, x.StudentId});
builder.Entity<Parents>()
.Property(p => p.Id)
.ValueGeneratedOnAdd();
builder.Entity<Parents>()
.HasKey(x => new { x.Id, x.StudentId});
}
}
然后,在我的 Startup.cs 文件中,我遵循了 Microsoft doc 所说的内容:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
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.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IStudentRepository, StudentRepository>();
services.AddMvc().AddNToastNotifyToastr();
services.AddJsReport(new LocalReporting()
.UseBinary(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? JsReportBinary.GetBinary()
: jsreport.Binary.OSX.JsReportBinary.GetBinary())
.AsUtility()
.Create());
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorPagesOptions(options =>
{
options.AllowAreas = true;
options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Identity/Account/Login";
options.LogoutPath = $"/Identity/Account/Logout";
options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseNToastNotify();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Student}/{action=Index}/{id?}");
});
}
}
有了以上所有信息,我的实现到底出了什么问题?
【问题讨论】:
标签: c# asp.net-core asp.net-mvc-scaffolding