我正在尝试将Identity
添加到我的项目中,以便添加登录功能(我正在学习在线课程)。我已经按照以下说明进行操作,但是在尝试添加迁移以使用更改更新数据库时遇到以下错误:
访问Microsoft.Extensions.Hosting时发生错误服务。没有应用程序服务提供商就继续进行。错误:无法加载文件或程序集'Microsoft.AspNetCore.Razor.Runtime,版本= 3.1.1.0,文化=中性,PublicKeyToken = adb9793829ddae60'。系统找不到指定的文件。
无法创建类型为'AppDbContext'的对象。对于不同设计时支持的模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728
我已经尝试在NuGet软件包中专门找到Microsoft.AspNetCore.Razor.Runtime
并添加它,但没有任何区别-任何人有任何想法吗?
遵循说明以至此:
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Identity.UI
AppDbContext
继承自Models.AppDbContext
的位置-更改public class AppDbContext : DbContext
至public class AppDbContext : IdentityDbContext<IdentityUser>
编辑:我的Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MLD.Models;
namespace MLD
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); // need to pass in the connection string here
services.AddControllersWithViews(); // brings in support for working with MVC in .Net Core
services.AddScoped<Repository>();
services.AddHttpContextAccessor();
services.AddSession(); // brings in session capability
}
// 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();
}
app.UseHttpsRedirection(); // redirects HTTP to HTTPS
app.UseStaticFiles(); // makes sure it serves JS, CSS, images
app.UseRouting();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=LymphSite}/{action=Index}/{id?}");
});
}
}
}
嗯,要使用身份,您应该将身份添加到services
:
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
我们还需要添加middleware
。我们插入中间件的位置很重要。添加以下代码之前 app.UseEndpoints
中间件。
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=LymphSite}/{action=Index}/{id?}");
});
您可以在此Identity
中找到有关如何配置link的更多说明。
让我知道发生了什么。祝你好运。