在.NET Core项目中添加Identity时出现NullReferenceException

发布于 2025-01-14 11:24:06 字数 1824 浏览 3 评论 0原文

我有一个 ASP.NET Core 6 Web 应用程序,它使用 EF Core 进行数据库管理,并使用 ASP.NET Identity 功能来管理用户。这个网络应用程序运行没有问题。

现在,我想开发一个独立的控制台应用程序,用于在数据库上执行维护任务(播种、清理操作、恢复出厂设置等),

但是我遇到了问题。 我已在维护应用程序的初始化步骤中添加了与我在 Web 应用程序上配置 EF Core 和 Itentity 时使用的相同指令,这些指令非常简单:

private static IHost CreateHost(string[] args)
{
    var builder = Host.CreateDefaultBuilder(args);
    builder.ConfigureServices((_, services) =>
        {
            services.AddDbContext<MyContext>(options =>
            {
                options.UseSqlite(connectionString);
            });

            services.AddDefaultIdentity<MyUser>(opt => { });
            services.AddScoped<MyDbService>();
        });
    return builder.Build();
}

但是,虽然这些指令在我的 Web 应用程序中可以正常工作,但在控制台应用程序中却不能。特别是,在调用 services.AddDefaultIdentity(opt => { }); 时,我收到 NullReferenceException

消息中没有内部异常或其他信息。堆栈跟踪是这样的:

   at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.GetApplicationAssembly(IdentityBuilder builder)
   at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.<>c__DisplayClass0_0.<AddDefaultUI>b__0(ApplicationPartManager apm)
   at Microsoft.Extensions.DependencyInjection.MvcCoreMvcBuilderExtensions.ConfigureApplicationPartManager(IMvcBuilder builder, Action`1 setupAction)
   at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddDefaultUI(IdentityBuilder builder)
   at Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity[TUser](IServiceCollection services, Action`1 configureOptions)
   at MainetnanceApp.Program.<>c.<CreateHost>b__2_0(HostBuilderContext _, IServiceCollection services)

为什么会发生这种情况?这是否与它是控制台应用程序而不是网络应用程序有关?我该如何解决这个问题?

I have a ASP.NET Core 6 webapp that uses EF Core for its database management and ASP.NET Identity functionality to manage users. This webapp works without issues.

Now, I wanted to develop a standalone console application that is used to perform maintenance tasks on the Database (seeding, cleanup operations, factory reset, etc.)

However I'm running into a problem.
I've added to the maintenance app's initialization steps the same instructions I use on the webapp to configure EF Core and Itentity, which are very simple:

private static IHost CreateHost(string[] args)
{
    var builder = Host.CreateDefaultBuilder(args);
    builder.ConfigureServices((_, services) =>
        {
            services.AddDbContext<MyContext>(options =>
            {
                options.UseSqlite(connectionString);
            });

            services.AddDefaultIdentity<MyUser>(opt => { });
            services.AddScoped<MyDbService>();
        });
    return builder.Build();
}

however, while these instructions work correctly in my webapp, in the console app they don't. In particular, I get a NullReferenceException when invoking services.AddDefaultIdentity<MyUser>(opt => { });

There is no inner exception or other info in the message. The stack trace is like this:

   at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.GetApplicationAssembly(IdentityBuilder builder)
   at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.<>c__DisplayClass0_0.<AddDefaultUI>b__0(ApplicationPartManager apm)
   at Microsoft.Extensions.DependencyInjection.MvcCoreMvcBuilderExtensions.ConfigureApplicationPartManager(IMvcBuilder builder, Action`1 setupAction)
   at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddDefaultUI(IdentityBuilder builder)
   at Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity[TUser](IServiceCollection services, Action`1 configureOptions)
   at MainetnanceApp.Program.<>c.<CreateHost>b__2_0(HostBuilderContext _, IServiceCollection services)

Why is this happening? Does it have something to do with the fact that it is a console app rather than a webapp? How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2025-01-21 11:24:06

AddDefaultIdentity 适用于网络应用程序,而不是控制台应用程序。
它添加了 UI、令牌提供程序,并配置身份验证以使用身份 cookie。

对于控制台应用程序,您可以使用 AddIdentityCore 相反。

AddDefaultIdentity is meant for a web app and not a console App.
It adds UI, token providers, and configures authentication to use identity cookies.

For console apps, you can use AddIdentityCore instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文