如何解决在方法'AddApiAuthorization <TUser,TContext>'的通用类型中,类型UserDBContext不能用作类型参数'TContext'
我正在尝试为我的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>();
解决方法
尝试类似的事情:
public class UserDBContext : IdentityDbContext,IPersistedGrantDbContext {
public DbSet<User> users { get; set; }
public UserDBContext (DbContextOptions<UserDBContext> options) : base (options) { }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。