Custom Int PK RoleManager Роли со свойством Users пусто - PullRequest
0 голосов
/ 08 февраля 2019

Я настроил Asp.Net Identity с помощью Int PK, поэтому я создал пользовательские классы, специфицирующие int как тип ключа. Код основан на http://bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/, это руководство для Roles и Claims Auth работает отлично.

 public class UserRoleIntPk : IdentityUserRole<int>
{
}
public class RoleIntPk : IdentityRole<int, UserRoleIntPk>
{

}
public class RoleStoreIntPk : RoleStore<RoleIntPk, int, UserRoleIntPk>

{
    public RoleStoreIntPk(ApplicationDbContext context)
        : base(context)
    {
    }
}
public class UserStoreIntPk : UserStore<ApplicationUser,RoleIntPk,int,UserLoginIntPk,UserRoleIntPk,UserClaimIntPk>

{
    public UserStoreIntPk(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class UserLoginIntPk : IdentityUserLogin<int>
{
}
public class UserClaimIntPk : IdentityUserClaim<int>
{
}
public class AuthManagers : UserManager<ApplicationUser,int>
{
    public AuthManagers(IUserRoleStore<ApplicationUser, int> userStore )
        : base(userStore)
    {
    }

    public static AuthManagers Create(IdentityFactoryOptions<AuthManagers> options, IOwinContext context)
    {
        var appDbContext = context.Get<ApplicationDbContext>();
        var appUserManager = new AuthManagers(new UserStoreIntPk(appDbContext));
        //Rest of code is removed for clarity
        appUserManager.EmailService = new EmailService();

        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            appUserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser,int>(dataProtectionProvider.Create("ASP.NET Identity"))
            {
                //Code for email confirmation and reset password life time
                TokenLifespan = TimeSpan.FromHours(6)
            };
        }
        appUserManager.UserValidator = new UserValidator<ApplicationUser,int>(appUserManager)
        {
            AllowOnlyAlphanumericUserNames = true,
            RequireUniqueEmail = true,
        };
        //Configure validation logic for passwords
        appUserManager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = false,
            RequireUppercase = false,
        };
        return appUserManager;
    }

}
public class ApplicationRoleManager : RoleManager<RoleIntPk, int>
{
    public ApplicationRoleManager(IRoleStore<RoleIntPk, int> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        var appRoleManager = new ApplicationRoleManager(new RoleStoreIntPk(context.Get<ApplicationDbContext>()));

        return appRoleManager;
    }

}

Мне чего-то не хватает или какая-либо базовая реализация мне не хватает?Я мог бы просто использовать Entities и создать собственную хранимую процедуру для этого, но так как свойство users уже существует, я подумал, что это будет излишним.Вот мои 2 класса, где у меня есть пользовательские модели идентичности и менеджеры

Проблема возникает в RoleController в маршруте "GetAllRoles"

var roles = AppRoleManager.Roles;

, который выдает следующий вывод JSON:

[
{
    "users": [],
    "id": 2,
    "name": "Admin"
},
{
    "users": [],
    "id": 1,
    "name": "SuperAdmin"
},
{
    "users": [],
    "id": 3,
    "name": "User"
}]

Проблема заключается в том, что свойство users является пустым, когда предполагается, что оно содержит идентификатор пользователя в указанных ролях.

...