Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException: Unable to resolve service for type 'OpenIddict.Core.OpenIddictApplicationManager`1[OpenIddict.Models.OpenIddictApplication]' while attempting to activate 'OpPISWeb.Controllers.AuthorizationController'. #347

Closed
MaklaCof opened this issue Jan 28, 2017 · 2 comments
Labels

Comments

@MaklaCof
Copy link

I am trying to build login based on RefreshFlow sample. I have Angular2 application. When I try to login I get the following error:

An unhandled exception occurred while processing the request.

InvalidOperationException: Unable to resolve service for type 'OpenIddict.Core.OpenIddictApplicationManager`1[OpenIddict.Models.OpenIddictApplication]' while attempting to activate 'OpPISWeb.Controllers.AuthorizationController'.
Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

I am using Javascript services template. My request is in attached file.
openiddict-error

My configuration is:

        public void ConfigureServices(IServiceCollection services)
        {
            try
            {
                services.AddMvc();

                services.AddEntityFrameworkSqlServer();

                services.AddScoped<UserStore<AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>, AppUserStore>();
                services.AddScoped<UserManager<AppUser>, AppUserManager>();
                services.AddScoped<RoleManager<AppRole>, AppRoleManager>();
                services.AddScoped<SignInManager<AppUser>, AppSignInManager>();
                services.AddScoped<RoleStore<AppRole, AppDbContext, int, AppUserRole, AppRoleClaim>, AppRoleStore>();

                var connection = Configuration["ConnectionStrings"];
                services.AddDbContext<AppDbContext>(options => {
                    options.UseSqlServer(connection);
                    options.UseOpenIddict<int>();
                });

                services
                    .AddIdentity<AppUser, AppRole>()
                    .AddUserStore<AppUserStore>()
                    .AddUserManager<AppUserManager>()
                    .AddRoleStore<AppRoleStore>()
                    .AddRoleManager<AppRoleManager>()
                    .AddSignInManager<AppSignInManager>()
                    .AddDefaultTokenProviders();

                services.AddOpenIddict<int>()
                    .AddEntityFrameworkCoreStores<AppDbContext>()
                    .AddMvcBinders()
                    .EnableTokenEndpoint("/API/authorization/token")
                    .AllowPasswordFlow()
                    .AllowRefreshTokenFlow()
                    .DisableHttpsRequirement();

                services.AddSingleton<DbSeeder>();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, DbSeeder dbSeeder)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true
                });
            }
            else
            {

            }

            app.UseStaticFiles();

            app.UseIdentity();

            app.UseOpenIddict();

            app.UseOAuthValidation();

            app.UseMvc();

            try
            {

                dbSeeder.SeedAsync();
            }
            catch (AggregateException e)
            {
                throw new Exception(e.ToString());
            }
        }

I have custom Identity classes and stores:

    [Table("Roles")]
    public partial class AppRole : IdentityRole<int, AppUserRole, AppRoleClaim>
    {
        public AppRole() { }
        public AppRole(string role) : base(role) { }
    }

    [Table("Users")]
    public partial class AppUser : IdentityUser<int, AppUserClaim, AppUserRole, AppUserLogin>
    {
    }

public partial class AppUserClaim : IdentityUserClaim<int>

...

public partial class AppDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>


    public class AppRoleManager : RoleManager<AppRole>
    {
        public AppRoleManager(IRoleStore<AppRole> store, IEnumerable<IRoleValidator<AppRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<AppRole>> logger, IHttpContextAccessor contextAccessor) : base(store, roleValidators, keyNormalizer, errors, logger, contextAccessor)
        {
        }
    }
    public class AppRoleStore : RoleStore<AppRole, AppDbContext, int, AppUserRole, AppRoleClaim>
    {
        public AppRoleStore(AppDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
        {
        }

        protected override AppRoleClaim CreateRoleClaim(AppRole role, Claim claim)
        {
            return new AppRoleClaim(role, claim);
        }
    }
    public class AppSignInManager : SignInManager<AppUser>
    {
        public AppSignInManager(UserManager<AppUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<AppUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<AppUser>> logger) : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger)
        {
        }
    }
    public class AppUserManager : UserManager<AppUser>
    {
        public AppUserManager(IUserStore<AppUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<AppUser> passwordHasher, IEnumerable<IUserValidator<AppUser>> userValidators, IEnumerable<IPasswordValidator<AppUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<AppUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
        }
    }
    public class AppUserStore : UserStore<AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>
    {
        public AppUserStore(AppDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
        {
        }

        protected override AppUserClaim CreateUserClaim(AppUser user, Claim claim)
        {
            return new AppUserClaim(user, claim);
        }

        protected override AppUserLogin CreateUserLogin(AppUser user, UserLoginInfo login)
        {
            return new AppUserLogin(user, login);
        }

        protected override AppUserRole CreateUserRole(AppUser user, AppRole role)
        {            
            return new AppUserRole(user, role);
        }

        protected override AppUserToken CreateUserToken(AppUser user, string loginProvider, string name, string value)
        {
            return new AppUserToken(user, loginProvider, name, value);
        }
    }

Controller:

 public class AuthorizationController : Controller
//...

[HttpPost("token"), Produces("application/json")]
        public async Task<IActionResult> Exchange(OpenIdConnectRequest request)
        {
            Debug.Assert(request.IsTokenRequest(),
                "The OpenIddict binder for ASP.NET Core MVC is not registered. " +
                "Make sure services.AddOpenIddict().AddMvcBinders() is correctly called.");
//....

I really hope someone can point me in right direction. I am desperate. Thank you.

@kevinchalet
Copy link
Member

To make them more visible for the community, questions should be posted on StackOverflow: https://stackoverflow.com/questions/tagged/openiddict.

See you there.

@MaklaCof
Copy link
Author

I guess so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants