Change User Id type to int in ASP.NET Identity in VS2015 (2)
IdentityModels.cs change to this:
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 : UserStoreint
,
UserLogin, UserRole, UserClaim>
{
public UserStore(ApplicationDbContext context): base(context)
{
}
}
public
class RoleStore : RoleStore
int, UserRole>
{
public RoleStore(ApplicationDbContext context): base(context)
{
}
}
public
class ApplicationUser : IdentityUser<
int, UserLogin, UserRole, UserClaim>
{
public DateTime? ActiveUntil;
public async Task
GenerateUserIdentityAsync(ApplicationUserManager manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContextint,
UserLogin, UserRole, UserClaim>
{
public ApplicationDbContext()
: base(
"DefaultConnection")
{
}
public
static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
In `App_Start\IdentityConfig.cs, change the following classes:
public class ApplicationUserManager : UserManagerint
>
{
public ApplicationUserManager(IUserStore
int> store)
: base(store)
{
}
public
static ApplicationUserManager Create(IdentityFactoryOptions
options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore(context.Get()));
manager.UserValidator = new UserValidatorint>(manager)
{
AllowOnlyAlphanumericUserNames =
false,
RequireUniqueEmail =
true
};
manager.PasswordValidator = new PasswordValidator
{
RequiredLength =
8,
RequireDigit =
true,
RequireLowercase =
true,
RequireUppercase =
true,
};
manager.UserLockoutEnabledByDefault =
true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(
5);
manager.MaxFailedAccessAttemptsBeforeLockout =
5;
manager.RegisterTwoFactorProvider(
"Phone Code", new PhoneNumberTokenProvider
int>
{
MessageFormat =
"Your security code is {0}"
});
manager.RegisterTwoFactorProvider(
"Email Code", new EmailTokenProvider
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
int>(dataProtectionProvider.Create(
"ASP.NET Identity"));
}
return manager;
}
}
public
class ApplicationSignInManager : SignInManager
int>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task
CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager(), context.Authentication);
}
}
In App_Start\Startup.Auth.cs change OnValidateIdentity property to this:
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: id => id.GetUserId())
Change ManageController to work with the new pk type:
Replace all entries of User.Identity.GetUserId() to User.Identity.GetUserId()
There might be a couple of string id arguments that need to be changed to int, but that's about it.