2015-12-29 8 views
16

में एएसपी.NET पहचान में int में उपयोगकर्ता आईडी प्रकार बदलें, डिफ़ॉल्ट रूप से वीएस 2015 में एएसपी.NET पहचान एस्पनेट *** टेबल के लिए प्राथमिक कुंजी के रूप में स्ट्रिंग का उपयोग करती है। मैं इसके बजाय int टाइप टाइप आईडी का उपयोग करना चाहता था। कुछ शोध के बाद यह पता चला कि अलग-अलग टाइप आईडी को बॉक्स के ढांचे से समर्थित किया जाता है। नीचे दिए गए जवाब में मैं दिखाऊंगा कि इसे प्राप्त करने के लिए क्या परिवर्तन किए जाएंगे।वीएस2015

अद्यतन: http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity

उत्तर

28
  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(); 
        } 
    } 
    
  2. में मेरा उत्तर जोड़ने के बाद मुझे लगता है कि एक ही है, लेकिन और अधिक व्यापक वर्णन करता है asp.net साइट पर इस ब्लॉग पोस्ट पाया `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); 
        } 
    } 
    
  3. App_Start\Startup.Auth.cs परिवर्तन इस के लिए OnValidateIdentity संपत्ति में:

को User.Identity.GetUserId<int>()

एक हो सकती है User.Identity.GetUserId() की सभी प्रविष्टियों को बदलें:

ManageController
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
    validateInterval: TimeSpan.FromMinutes(30), 
    regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), 
    getUserIdCallback: id => id.GetUserId<int>()) 
  • बदलें नई pk प्रकार के साथ काम करने के लिए स्ट्रिंग id तर्क जो कि int में बदलने की आवश्यकता है, लेकिन यह इसके बारे में है।

  • +0

    यदि आप वास्तव में हाइलाइट कर सकते हैं कि आपका कोड int आईडी बना रहा था जो महान थाक्स होगा। – niico

    +0

    क्या हमें उपयोगकर्ता तालिका के दायर आईडी पर ऑटो वृद्धि को सक्षम करना चाहिए ?? – Reza

    +0

    @niico - जहां तक ​​मुझे याद है कि एथ डीबी में आईडी फ़ील्ड ऑटो वृद्धि – Andrey

    6

    प्रति this blog post, ASP.NET कोर पहचान के साथ, निम्न परिवर्तन करें:

    पहले, Data\Migrations फ़ोल्डर में जाएँ और वहाँ में सब कुछ हटा।

    Startup.cs में, ConfigureServices विधि में, services.AddIdentity

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

    को IdentityDbContext<ApplicationUser> से आधार वर्ग

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

    के अंत में बदल ApplicationDbContext.cs परिवर्तन में, IdentityUser से

    को ApplicationUser.cs में आधार वर्ग बदलने
    public class ApplicationUser : IdentityUser<int> 
    

    फिर add-migration -o Data\Migrations और update-database चलाएं। यदि माइग्रेशन किसी भी समस्या का कारण बनता है, तो डेटाबेस को हटाने के लिए VS में SQL सर्वर प्रबंधन स्टूडियो या SqlServerObjectExplorer का उपयोग करें ( केवल फ़ाइल सिस्टम का उपयोग करें), अपने माइग्रेशन को दोबारा हटाएं, और पुनः प्रयास करें।

    संबंधित मुद्दे