1

我正在尝试基于RefreshFlow 示例构建登录。我有 Angular2 应用程序。当我尝试登录时,出现以下错误:

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)

我正在使用Javascript 服务模板。我的请求在 Github 上的附件中

我的配置是:

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());
    }
}

我有自定义身份类和商店:

   [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);
        }
    }

控制器:

 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.");
//....

我真的希望有人能指出我正确的方向。生无可恋。谢谢你。

4

1 回答 1

2

由于您使用自定义键类型 ( int),因此请确保您还使用正确的实体:(OpenIddictApplication<int>而不是OpenIddictApplication)。

OpenIddictApplicationManager<OpenIddictApplication>

-->

OpenIddictApplicationManager<OpenIddictApplication<int>>
于 2017-01-28T21:15:38.383 回答