簡體   English   中英

該類型不能用作泛型類型或方法UserStore中的類型參數TRole

[英]The type cannot be used as type parameter TRole in the generic type or method UserStore

我正在嘗試在Visual Studio 2015中使用IdentityServer4和ASP.NET Core MVC 1.0.1構建Identity Server。我已經使用EntityFramework Core 1.0.1( 第8節 )成功設置了ConfigurationStoreOperationalStore

這是我的project.json文件的樣子(部分):

{
    "dependencies": {
        "Microsoft.NETCore.App" {
            "version": "1.0.1",
            "type": "platform"
        },
        "Microsoft.AspNetCore.Mvc": "1.0.1",
        /////// Entity Framework /////////
        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1", 
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
        /////// ASP.NET Identity /////////
        "Microsoft.AspNetCore.Identity": "1.0.0",
        "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
        /////// Identity Server //////////
        "IdentityServer4": "1.0.0-rc1-update2",
        "IdentityServer4.AspNetIdentity": "1.0.0-rc1-update2",
        "IdentityServer4.EntityFramework": "1.0.0-rc1-update2",
        ......
    }
}

我試圖將ASP.NET Identity用作第6節中所述的Identity Server中的用戶存儲。 默認情況下,ASP.NET身份使用string作為所有表的鍵,但是我想使用int所以我像這樣更改它們:

public class AppUserToken : IdentityUserToken<int> { }
public class AppUserLogin : IdentityUserLogin<int> { }
public class AppUserClaim : IdentityUserClaim<int> { }
public class AppUserRole : IdentityUserRole<int> { }
public class AppRoleClaim : IdentityRoleClaim<int> { }
public class AppRole : IdentityRole<int, AppUserRole, AppRoleClaim> { }
public class AppUser : IdentityUser<int, AppUserClaim, AppUserRole, AppUserLogin>

然后,我需要像這樣創建自定義用戶和角色存儲:

public class AppRoleStore : RoleStore<AppRole, AppIdentityDbContext, int, AppUserRole, AppRoleClaim>
{
    public AppRoleStore(AppIdentityDbContext context, IdentityErrorDescriber describer = null)
        : base(context, describer)
    {}

    .......
}

public class AppUserStore : UserStore<AppUser, AppRole, AppIdentityDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken>
{
    public AppUserStore(AppIdentityDbContext context, IdentityErrorDescriber describer = null) 
        : base(context, describer)
    { }

    ............
}

最后但並非最不重要的是,我的AppIdentityDbContext

public class AppIdentityDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
    public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
        : base(options)
    { }

    public override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<AppUser>().ToTable("Users");
        builder.Entity<AppUserLogin>().ToTable("UserLogins");
        builder.Entity<AppUserClaim>().ToTable("UserClaims");
        builder.Entity<AppUserToken>().ToTable("UserTokens");

        builder.Entity<AppRole>().ToTable("Roles");
        builder.Entity<AppRoleClaim>().ToTable("RoleClaims");

        builder.Entity<AppUserRole>().ToTable("UserRoles");
    }
}

但是我在AppUserStoreAppUserStore構建錯誤: AppUserStore 類型不能用作通用類型或方法“ UserStore”中的類型參數“ TRole”。 沒有從“ AppRole”到“ Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>”的隱式引用轉換。

wtf?

您正在兩次描述IdentityUserRole :對於Role和對於UserRole

public class AppUserRole : IdentityUserRole<int> { }
public class AppRole :     IdentityUserRole<int, AppUserRole, AppRoleClaim> { }

您的AppRole應該繼承自IdentityRole ,而不是IdentityUserRole

public class AppRole : IdentityRole...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM