Nach dem gleichen Quellcode und schreiben Sie den Code selbst gehen Annahmen jetzt. Zuerst erstellen Sie ein neues Projekt in vs mit mvc (C#) und wählen den Projekttyp auf mvc. Dann, nach dem Erstellen des Projekts sollten Sie einige Klassen Identität ändern. Ich möchte zu UserId
ändern, die Nvarchar(128)
zu Bigint
in Tabelle ist. Schritt eins bis sieben sind in IdentityModels.cs
.
Schritt eins: Sie Klasse ändern IdentityModels.cs
zu der nach:
ApplicationUser : IdentityUser
Änderung: ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
.
Schritt zwei: Erstellen von Klassen:
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
Schritt drei: Änderung Klasse nach dem:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
Schritt vier: Änderung Klasse nach dem:
public class CustomRole : IdentityRole<long, CustomUserRole>
Schritt fünf: Ändern Sie die Klasse gemäß:
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
Schritt sechs: Änderung Klasse nach dem:
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
Schritt sieben: Änderung Klasse nach dem:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
und fügt Methode unten an IdentityModels.cs
nach Ihrem Tabellenfeld:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Entity<CustomUserLogin>()
//.HasKey(r => new { r.UserId })
//.ToTable("tblRoles");
//modelBuilder.Entity<CustomUserLogin>()
// .HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
// .ToTable("tblMembers");
modelBuilder.Entity<IdentityUser>().HasKey(r => new { r.Id }).ToTable("tblMembers1").Property(p => p.Id).HasColumnName("UserID");
modelBuilder.Entity<ApplicationUser>().HasKey(r => new { r.Id }).ToTable("tblMembers").Property(p => p.Id).HasColumnName("UserID");
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId }).ToTable("tblRoless");
modelBuilder.Entity<IdentityUserLogin>().HasKey(r => new { r.UserId }).ToTable("tblLogins");
modelBuilder.Entity<IdentityUserClaim>().HasKey(r => new { r.Id }).ToTable("tblUserClaims");
modelBuilder.Entity<IdentityRole>().HasKey(r => new { r.Id }).ToTable("tblRoloess");
}
In AccountControllers.cs
cha nge nach dem:
private bool HasPassword()
{
var user = UserManager.FindById(User.Identity.GetUserId<long>());
if (user != null)
{
return user.PasswordHash != null;
}
return false;
}
public virtual ActionResult RemoveAccountList()
{
var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId<long>());
ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1;
return PartialView("_RemoveAccountPartial", linkedAccounts);
}
public virtual async Task<ActionResult> Disassociate(string loginProvider, string providerKey)
{
ManageMessageId ? message = null;
IdentityResult result = await UserManager.RemoveLoginAsync(
User.Identity.GetUserId<long>(),
new UserLoginInfo(loginProvider, providerKey));
if (result.Succeeded)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<long>());
await SignInAsync(user, isPersistent: false);
message = ManageMessageId.RemoveLoginSuccess;
}
else
{
message = ManageMessageId.Error;
}
return RedirectToAction("Manage", new { Message = message });
}
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
AuthenticationManager.SignIn(new AuthenticationProperties()
{ IsPersistent = isPersistent },
await user.GenerateUserIdentityAsync(UserManager));
}
Und IdentityConfig.cs
Änderung nach dem:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
return Task.FromResult(0);
}
}
public class SmsService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your SMS service here to send a text message.
return Task.FromResult(0);
}
}
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser, long>
{
public ApplicationUserManager(IUserStore<ApplicationUser, long> store) : base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(
new CustomUserStore(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser, long>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// 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 in here.
manager.RegisterTwoFactorProvider("PhoneCode",
new PhoneNumberTokenProvider<ApplicationUser, long>
{
MessageFormat = "Your security code is: {0}"
});
manager.RegisterTwoFactorProvider("EmailCode",
new EmailTokenProvider<ApplicationUser, long>
{
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, long>(
dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
// Configure the application sign-in manager which is used in this application.
}
public class ApplicationSignInManager : SignInManager<ApplicationUser, long>
{
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);
}
}
Dann in Fenster Package Manager Console in Menü Extras Schreib unten für Add Migrationsbefehle:
Enable migrations
Dann:
Add-migration TestMig
Nach dem Bild: enter image description here
Mit freundlichen Grüßen.
Bitte stimme zu meiner Antwort. :)) – MojtabaNava
Danke für deine Antwort, aber leider hat es nicht auf meine Frage geantwortet. – Fabio
Sie sollten Codes in Ihrem Projekt verwenden. :)) – MojtabaNava