Измените тип идентификатора пользователя на int в ASP.NET Identity в VS2015

По умолчанию ASP.NET Identity в VS 2015 использует строку в качестве первичного ключа для таблиц AspNet ***. Вместо этого я хотел использовать int-typed id. После некоторых исследований выяснилось, что различные типизированные идентификаторы поддерживаются каркасом из коробки. В ответе ниже я покажу, какие изменения нужно сделать для достижения этого.

UPDATE: после добавления ответа я нашел этот пост в блоге на сайте asp.net, который описывает то же самое, но более всеобъемлющее: http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity

Ответы

Ответ 1

  • IdentityModels.cs измените на это:

    // New derived classes
    public class UserRole : IdentityUserRole<int>
    {
    }
    
    public class UserClaim : IdentityUserClaim<int>
    {
    }
    
    public class UserLogin : IdentityUserLogin<int>
    {
    }
    
    public class Role : IdentityRole<int, UserRole>
    {
        public Role() { }
        public Role(string name) { Name = name; }
    }
    
    public class UserStore : UserStore<ApplicationUser, Role, int,
        UserLogin, UserRole, UserClaim>
    {
        public UserStore(ApplicationDbContext context): base(context)
        {
        }
    }
    
    public class RoleStore : RoleStore<Role, int, UserRole>
    {
        public RoleStore(ApplicationDbContext context): base(context)
        {
        }
    }
    
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser<int, UserLogin, UserRole, UserClaim>
    {
        public DateTime? ActiveUntil;
    
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, int,
        UserLogin, UserRole, UserClaim>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
    
  • В `App_Start\IdentityConfig.cs 'измените следующие классы:

    public class ApplicationUserManager : UserManager<ApplicationUser, int>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
            : base(store)
        {
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
    
            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 8,
                // RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };
    
            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;
    
            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser, int>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser, int>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = 
                    new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }
    
    // Configure the application sign-in manager which is used in this application.
    public class ApplicationSignInManager : SignInManager<ApplicationUser, int>
    {
        public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager)
        {
        }
    
        public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
        {
            return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
        }
    
        public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }
    }
    
  • В App_Start\Startup.Auth.cs измените свойство OnValidateIdentity на это:

    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
        validateInterval: TimeSpan.FromMinutes(30),
        regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
        getUserIdCallback: id => id.GetUserId<int>())
    
  • Измените ManageController для работы с новым типом pk:

Заменить все записи User.Identity.GetUserId() на User.Identity.GetUserId<int>()

Может быть несколько аргументов строки id, которые нужно изменить на int, но это о нем.

Ответ 2

Per этот пост в блоге с Идентификатором основного кода ASP.NET внесите следующие изменения:

Сначала перейдите в папку Data\Migrations и удалите все.

В Startup.cs в методе ConfigureServices измените services.AddIdentity на

services.AddIdentity<ApplicationUser, IdentityRole<int>>()
   .AddEntityFrameworkStores<ApplicationDbContext, int>()
   .AddDefaultTokenProviders();

В ApplicationDbContext.cs измените базовый класс с IdentityDbContext<ApplicationUser> на

public class ApplicationDbContext
  : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>

Наконец, измените базовый класс в ApplicationUser.cs с IdentityUser на

public class ApplicationUser : IdentityUser<int>

Затем запустите add-migration -o Data\Migrations и update-database. Если миграции вызывают какие-либо проблемы, используйте Sql Server Management Studio или SqlServerObjectExplorer в VS для удаления базы данных ( не, просто используйте файловую систему), повторно удалите свои миграции и повторите попытку.