locked
Run time error after config SignInManager in Startup.cs RRS feed

  • Question

  • User1772251356 posted

    Hello ,

    I tried to implement Login system by using Microsoft Identity.

    i got run time error and i am not able to find what kind error please help me.

    I am using Asp.Net core 3.1 and Miccroft.AspNetCore.Identity.EntityFrameworkCore 3.1.1

    Staturp.CS

    public void ConfigureServices(IServiceCollection services)
            {
                IdentityBuilder builder = services.AddIdentityCore<User>(options =>
                {
                    options.Password.RequireDigit = false;
                    options.Password.RequireLowercase = false;
                    options.Password.RequireNonAlphanumeric = false;
                    options.Password.RequireUppercase = false;
                    options.Password.RequiredLength = 6;
                });
                
                builder.AddEntityFrameworkStores<ApplicationDbContext>();
                builder.AddSignInManager<SignInManager<User>>();
    
    
                services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("Default")));
                services.AddControllers();
            }

    AuthController

      public UserManager<User> _userManager { get; set; }
           public SignInManager<User> _signInManager { get; set; }
          public AuthController(UserManager<User> userManager,SignInManager<User> signInManager)
          {
              _userManager = userManager;
              _signInManager = signInManager;
          }
            // POST api/auth/login
            [HttpPost("login")]
            public async Task<IActionResult> Login(LoginViewModel model)
            {
              var user = await _userManager.FindByNameAsync(model.UserName);
              var result = await _signInManager.CheckPasswordSignInAsync(user,model.Password,false);
              if(!result.Succeeded)
              {
                return BadRequest(result);
              }
            
              return Ok(result);
              
            }

    Run time Error is:

    Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator`1[Employee.DAL.Entities.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator`1[Employee.DAL.Entities.User]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[Employee.DAL.Entities.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[Employee.DAL.Entities.User]'.)
     ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator`1[Employee.DAL.Entities.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator`1[Employee.DAL.Entities.User]'.
     ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' 
    while attempting to activate 'Microsoft.AspNetCore.Identity.SecurityStampValidator`1[Employee.DAL.Entities.User]'.
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, 
    CallSiteChain callSiteChain)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
       --- End of inner exception stack trace ---
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
       at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions 
    options)
       --- End of inner exception stack trace ---
       at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions 
    options)
       at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
       at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
       at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
       at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
       at Microsoft.Extensions.Hosting.HostBuilder.Build()
       at Employee.API.Program.Main(String[] args) in D:\Project\Employee.API\Program.cs:line 16
     ---> (Inner Exception #1) System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[Employee.DAL.Entities.User]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[Employee.DAL.Entities.User]'.
     ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' 
    while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator`1[Employee.DAL.Entities.User]'.
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, 
    CallSiteChain callSiteChain)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
       --- End of inner exception stack trace ---
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
       at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions 
    options)<---
    
    

    Wednesday, April 1, 2020 4:24 PM

Answers

  • User-474980206 posted

    because you did not user the helper 

        services.AddDefaultIdentity<>()

    but called 

        services.AddIdentityCore<>()

    you need to register all the required services yourself.

    as suggested looking at a new project template is the simplest way to know what to do.</div> <div></div> </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2020 7:52 PM

All replies

  • User475983607 posted

    Create a new project and scaffold Identity.  Then compare the template code with your configuration.

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.1&tabs=visual-studio

    The startup will look similar to the following.

                services.AddDbContext<ApplicaitonContext>(options =>
                        options.UseSqlServer(
                            Configuration.GetConnectionString("ApplicaitonContextConnection")));
    
                services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<ApplicaitonContext>();

    Wednesday, April 1, 2020 5:18 PM
  • User-474980206 posted

    because you did not user the helper 

        services.AddDefaultIdentity<>()

    but called 

        services.AddIdentityCore<>()

    you need to register all the required services yourself.

    as suggested looking at a new project template is the simplest way to know what to do.</div> <div></div> </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2020 7:52 PM
  • User1772251356 posted

    After added  services.AddAuthentication() i didn't get any run time error. 

    I followed your given instruction i fixed that issue. 

    thank you so much.

    Thursday, April 2, 2020 6:23 AM