Reputation: 336
When I run
dotnet ef migrations add IdentityInitial -p Data -s SignalRreactjs -c IdentityAppDbContext -o Identity/Migrations
I get this response back
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (E rror while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.Secu rityStampValidator
1[Data.Models.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Ide ntity.SecurityStampValidator
1[Data.Models.AppUser]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifet ime: Scoped ImplementationType: 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.TwoFactorSecurityStampValidator
1[Data.Models.AppUser]'.) Unable to create an object of type 'IdentityAppDbContext'. For the different patterns supported at design time, see 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>
Upvotes: 0
Views: 667
Reputation: 3646
The problem is that you're using AddIdentityCore()
, which (unlike AddIdentity()
or AddDefaultIdentity()
, all of which are confusing btw) doesn't contain a call to AddAuthentication()
. Since ISystemClock
is added by AddAuthentication()
.
So you need to add a call yourself, preferably below AddIdentityCore()
:
services.AddAuthentication();
Possibly you'll also need to provide your authentication configuration, depending on your authentication requirements.
Alternatively, you can replace AddIdentityCore()
with AddIdentity()
or AddDefaultIdentity()
. That way the problem won't occur, and you'll have authentication configured with Identity. But that configuration includes possibly annoying cookies and redirection, and it's generally only good when you're working on an MVC project (i.e. not web API).
Upvotes: 1
Reputation: 1100
You can try registering ISystemClock
in your ConfigureServices
method in the startup class.
services.TryAddSingleton<ISystemClock, SystemClock>();
But it is not recommended. Similar StackOverflow Question
Upvotes: 0