使用EntityFrameworkCore向ASP.NET Core项目添加身份的问题

问题描述 投票:0回答:1

我正在尝试将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并添加它,但没有任何区别-任何人有任何想法吗?


遵循说明以至此:

  • 转到依赖项>管理NuGet程序包
  • 添加以下软件包:
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore

  • Microsoft.AspNetCore.Identity.UI

  • 更改AppDbContext继承自Models.AppDbContext的位置-更改public class AppDbContext : DbContextpublic 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?}");
            });
        }
    }
}
asp.net-core entity-framework-core asp.net-core-identity
1个回答
0
投票

嗯,要使用身份,您应该将身份添加到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的更多说明。

让我知道发生了什么。祝你好运。

© www.soinside.com 2019 - 2024. All rights reserved.