【问题标题】:Error when adding identity migrations in asp.net core在 asp.net core 中添加身份迁移时出错
【发布时间】:2021-03-14 15:42:53
【问题描述】:

当我跑步时

dotnet ef migrations add IdentityInitial -p Data -s SignalRreactjs -c IdentityAppDbContext -o Identity/Migrations

我收到了回复

访问 Microsoft.Extensions.Hosting 时出错 服务。在没有应用程序服务提供商的情况下继续。错误: 某些服务无法构建(验证时出错 服务描述符'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator 生命周期:范围 实施类型:Microsoft.AspNetCore.Identity.Secu rityStampValidator1[Data.Models.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Ide ntity.SecurityStampValidator1[Data.Models.AppUser]'。)(出错时 验证服务描述符 'ServiceType: Microsoft.AspNetCore.Identity.IwoFactorSecurityStampValidator Lifet ime:范围实施类型: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Data.Models.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authenti cation.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Data.Models.AppUser]'。) 无法创建“IdentityAppDbContext”类型的对象。为了 设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728

Startup.cs

    public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<IdentityAppDbContext>(x =>
            {
                x.UseSqlServer(_config.GetConnectionString("IdentityConnection"));
            });

            services.AddIdentityCore<AppUser>().AddEntityFrameworkStores<IdentityAppDbContext>()
                .AddSignInManager<SignInManager<AppUser>>();

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {Title = "SignalRreactjs", Version = "v1"});
            });
        }

IdentityAppDbContext.cs

using Data.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Data.Identity
{
    public class IdentityAppDbContext : IdentityDbContext<AppUser>
    {
        public IdentityAppDbContext(DbContextOptions<IdentityAppDbContext> options) : base(options)
        {
            
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
}

AppUser.cs

using Microsoft.AspNetCore.Identity;

namespace Data.Models
{
    public class AppUser : IdentityUser
    {
        public string DisplayName { get; set; }

        public string ImageUrl { get; set; }
    }
}

Data.csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
      <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
      <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4" />
      <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.4" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
      <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
      <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
    </ItemGroup>

</Project>

SignalRreactjs.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-preview.2.21154.2">
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
          <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
    </ItemGroup>

    <ItemGroup>
      <Folder Include="Controllers" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\Data\Data.csproj" />
    </ItemGroup>

</Project>

【问题讨论】:

  • 嗨@lizardcoder,我想你可以查看this answer

标签: c# asp.net-core asp.net-web-api


【解决方案1】:

您可以尝试在启动类的ConfigureServices 方法中注册ISystemClock

services.TryAddSingleton&lt;ISystemClock, SystemClock&gt;();

但不推荐。 Similar StackOverflow Question

【讨论】:

    【解决方案2】:

    问题在于您使用的是AddIdentityCore(),它(与AddIdentity()AddDefaultIdentity() 不同,顺便说一句,所有这些都令人困惑)不包含对AddAuthentication() 的调用。因为ISystemClockadded by AddAuthentication()

    所以你需要自己添加一个调用,最好在AddIdentityCore()下面:

    services.AddAuthentication();
    

    您可能还需要提供您的身份验证配置,具体取决于您的身份验证要求。

    或者,您可以将AddIdentityCore() 替换为AddIdentity()AddDefaultIdentity()。这样问题就不会发生,并且您将使用 Identity 配置身份验证。但该配置可能包括烦人的 cookie 和重定向,并且通常仅在您处理 MVC 项目(即不是 Web API)时才有用。

    【讨论】:

    • 谢谢,我会尽快尝试解决方案
    • @lizardcoder 确定。 :) 如果您还有其他问题,请随时发表评论(或修改您的问题),我可以尝试通过编辑来解决这些问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多