我正在尝试为我的React ASP.NET Core应用程序使用身份设置授权。我正在关注ASP.NET Core文档:Docs
在前面的示例中,我在文档中引用“ ApplicationContext”的地方都使用了“ UserDBContext”,但在这种情况下我遇到了错误。
当我尝试配置'AddIdentityServer'服务时遇到错误。在我的Startup.cs
完全错误:
The type 'projectName.UserDBContext' cannot be used as type parameter 'TContext' in the generic type or method 'IdentityServerBuilderConfigurationExtensions.AddApiAuthorization<TUser, TContext>(IIdentityServerBuilder)'. There is no implicit reference conversion from 'projectName.UserDBContext' to 'IdentityServer4.EntityFramework.Interfaces.IPersistedGrantDbContext'
User.cs:
namespace project {
public class User : IdentityUser {
[DataType (DataType.Date)]
public DateTime created { get; set; }
[Key]
[Required]
public string email { get; set; }
[Required]
public string firstname { get; set; }
[Required]
public string lastname { get; set; }
[Required]
[Range (1, 100)]
public string username { get; set; }
[Required]
[StringLength (100)]
public string password { get; set; }
}
public class UserDBContext : IdentityDbContext {
public DbSet<User> users { get; set; }
public UserDBContext (DbContextOptions<UserDBContext> options) : base (options) { }
}
}
Startup.cs
public void ConfigureServices (IServiceCollection services) {
services.AddControllersWithViews ();
services.AddSpaStaticFiles (configuration => {
configuration.RootPath = "ClientApp/build";
});
services.AddDbContext<UserDBContext> (options =>
options.UseNpgsql (Configuration.GetConnectionString ("DefaultConnection")));
services.AddDefaultIdentity<User> ()
.AddEntityFrameworkStores<UserDBContext> ();
services.AddIdentityServer()
.AddApiAuthorization<User, UserDBContext>();
services.AddAuthentication ()
.AddIdentityServerJwt ();
}
这是给我错误的代码:
services.AddIdentityServer()
.AddApiAuthorization<User, UserDBContext>();
答案 0 :(得分:1)
尝试类似的事情:
public class UserDBContext : IdentityDbContext,IPersistedGrantDbContext {
public DbSet<User> users { get; set; }
public UserDBContext (DbContextOptions<UserDBContext> options) : base (options) { }
}