How do I create a relationship between AspNetUserRoles and some other table using EF6 code first?
up vote
0
down vote
favorite
I'm using ASP.Net Identity 2 and EF6 code first migrations for this project and I'm struggling to create a table to link 3 other tables. I have the following:
- Company
- ApplicationUser (AspNetUsers)
- ApplicationRole (AspNetRoles)
- AspNetUserRoles
The tricky bit is to relate the AspNetUserRoles with the Company table, because a user can have roles in different companies and can also have a role that's not related to any company. So ideally the resulting table I need would be something like:
- CompanyUserRoles(companyId, UserRoleId[this is not roleID]) or even (companyId, roleid,userid)
Another option would be to add another field (companyId) to the AspNetUserRoles table.
It's kind of confusing how can I achieve this with code first and what implications would it have when it comes to the UserManager and RoleManager (do I need to create custom ones?).
Any guidance would be greatly appreciated.
asp.net entity-framework entity-framework-6 asp.net-identity asp.net-roles
add a comment |
up vote
0
down vote
favorite
I'm using ASP.Net Identity 2 and EF6 code first migrations for this project and I'm struggling to create a table to link 3 other tables. I have the following:
- Company
- ApplicationUser (AspNetUsers)
- ApplicationRole (AspNetRoles)
- AspNetUserRoles
The tricky bit is to relate the AspNetUserRoles with the Company table, because a user can have roles in different companies and can also have a role that's not related to any company. So ideally the resulting table I need would be something like:
- CompanyUserRoles(companyId, UserRoleId[this is not roleID]) or even (companyId, roleid,userid)
Another option would be to add another field (companyId) to the AspNetUserRoles table.
It's kind of confusing how can I achieve this with code first and what implications would it have when it comes to the UserManager and RoleManager (do I need to create custom ones?).
Any guidance would be greatly appreciated.
asp.net entity-framework entity-framework-6 asp.net-identity asp.net-roles
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using ASP.Net Identity 2 and EF6 code first migrations for this project and I'm struggling to create a table to link 3 other tables. I have the following:
- Company
- ApplicationUser (AspNetUsers)
- ApplicationRole (AspNetRoles)
- AspNetUserRoles
The tricky bit is to relate the AspNetUserRoles with the Company table, because a user can have roles in different companies and can also have a role that's not related to any company. So ideally the resulting table I need would be something like:
- CompanyUserRoles(companyId, UserRoleId[this is not roleID]) or even (companyId, roleid,userid)
Another option would be to add another field (companyId) to the AspNetUserRoles table.
It's kind of confusing how can I achieve this with code first and what implications would it have when it comes to the UserManager and RoleManager (do I need to create custom ones?).
Any guidance would be greatly appreciated.
asp.net entity-framework entity-framework-6 asp.net-identity asp.net-roles
I'm using ASP.Net Identity 2 and EF6 code first migrations for this project and I'm struggling to create a table to link 3 other tables. I have the following:
- Company
- ApplicationUser (AspNetUsers)
- ApplicationRole (AspNetRoles)
- AspNetUserRoles
The tricky bit is to relate the AspNetUserRoles with the Company table, because a user can have roles in different companies and can also have a role that's not related to any company. So ideally the resulting table I need would be something like:
- CompanyUserRoles(companyId, UserRoleId[this is not roleID]) or even (companyId, roleid,userid)
Another option would be to add another field (companyId) to the AspNetUserRoles table.
It's kind of confusing how can I achieve this with code first and what implications would it have when it comes to the UserManager and RoleManager (do I need to create custom ones?).
Any guidance would be greatly appreciated.
asp.net entity-framework entity-framework-6 asp.net-identity asp.net-roles
asp.net entity-framework entity-framework-6 asp.net-identity asp.net-roles
asked Dec 1 '16 at 18:46
Fabio
5501421
5501421
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
According to the same source code and write the code yourself walking assumptions now.
First, you create new project in vs with mvc(c#) and select project type on mvc.
Then, after create project you should change some classes Identity.
I want change to type UserId
that has been Nvarchar(128)
to Bigint
in table AspNetUsers
.
Step one to seven are in IdentityModels.cs
.
step one: you change class IdentityModels.cs
to according to the:
ApplicationUser : IdentityUser
change to : ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
.
Step two: create classes:
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
step three:
change class according to the :
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
Step four: change class according to the :
public class CustomRole : IdentityRole<long, CustomUserRole>
Step five: change class according to the :
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
Step six: change class according to the :
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
Step seven: change class according to the :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
and add method below to IdentityModels.cs
according your table field:
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
change according to the :
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));
}
And IdentityConfig.cs
change according to the :
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);
}
}
Then, in windows Package Manager Console in Tools menu write commands below for add migration :
Enable migrations
Then :
Add-migration TestMig
According the image:
enter image description here
Best regards.
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40918199%2fhow-do-i-create-a-relationship-between-aspnetuserroles-and-some-other-table-usin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
According to the same source code and write the code yourself walking assumptions now.
First, you create new project in vs with mvc(c#) and select project type on mvc.
Then, after create project you should change some classes Identity.
I want change to type UserId
that has been Nvarchar(128)
to Bigint
in table AspNetUsers
.
Step one to seven are in IdentityModels.cs
.
step one: you change class IdentityModels.cs
to according to the:
ApplicationUser : IdentityUser
change to : ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
.
Step two: create classes:
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
step three:
change class according to the :
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
Step four: change class according to the :
public class CustomRole : IdentityRole<long, CustomUserRole>
Step five: change class according to the :
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
Step six: change class according to the :
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
Step seven: change class according to the :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
and add method below to IdentityModels.cs
according your table field:
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
change according to the :
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));
}
And IdentityConfig.cs
change according to the :
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);
}
}
Then, in windows Package Manager Console in Tools menu write commands below for add migration :
Enable migrations
Then :
Add-migration TestMig
According the image:
enter image description here
Best regards.
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
|
show 1 more comment
up vote
0
down vote
According to the same source code and write the code yourself walking assumptions now.
First, you create new project in vs with mvc(c#) and select project type on mvc.
Then, after create project you should change some classes Identity.
I want change to type UserId
that has been Nvarchar(128)
to Bigint
in table AspNetUsers
.
Step one to seven are in IdentityModels.cs
.
step one: you change class IdentityModels.cs
to according to the:
ApplicationUser : IdentityUser
change to : ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
.
Step two: create classes:
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
step three:
change class according to the :
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
Step four: change class according to the :
public class CustomRole : IdentityRole<long, CustomUserRole>
Step five: change class according to the :
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
Step six: change class according to the :
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
Step seven: change class according to the :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
and add method below to IdentityModels.cs
according your table field:
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
change according to the :
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));
}
And IdentityConfig.cs
change according to the :
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);
}
}
Then, in windows Package Manager Console in Tools menu write commands below for add migration :
Enable migrations
Then :
Add-migration TestMig
According the image:
enter image description here
Best regards.
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
According to the same source code and write the code yourself walking assumptions now.
First, you create new project in vs with mvc(c#) and select project type on mvc.
Then, after create project you should change some classes Identity.
I want change to type UserId
that has been Nvarchar(128)
to Bigint
in table AspNetUsers
.
Step one to seven are in IdentityModels.cs
.
step one: you change class IdentityModels.cs
to according to the:
ApplicationUser : IdentityUser
change to : ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
.
Step two: create classes:
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
step three:
change class according to the :
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
Step four: change class according to the :
public class CustomRole : IdentityRole<long, CustomUserRole>
Step five: change class according to the :
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
Step six: change class according to the :
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
Step seven: change class according to the :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
and add method below to IdentityModels.cs
according your table field:
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
change according to the :
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));
}
And IdentityConfig.cs
change according to the :
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);
}
}
Then, in windows Package Manager Console in Tools menu write commands below for add migration :
Enable migrations
Then :
Add-migration TestMig
According the image:
enter image description here
Best regards.
According to the same source code and write the code yourself walking assumptions now.
First, you create new project in vs with mvc(c#) and select project type on mvc.
Then, after create project you should change some classes Identity.
I want change to type UserId
that has been Nvarchar(128)
to Bigint
in table AspNetUsers
.
Step one to seven are in IdentityModels.cs
.
step one: you change class IdentityModels.cs
to according to the:
ApplicationUser : IdentityUser
change to : ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
.
Step two: create classes:
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
step three:
change class according to the :
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
Step four: change class according to the :
public class CustomRole : IdentityRole<long, CustomUserRole>
Step five: change class according to the :
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
Step six: change class according to the :
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
Step seven: change class according to the :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
and add method below to IdentityModels.cs
according your table field:
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
change according to the :
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));
}
And IdentityConfig.cs
change according to the :
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);
}
}
Then, in windows Package Manager Console in Tools menu write commands below for add migration :
Enable migrations
Then :
Add-migration TestMig
According the image:
enter image description here
Best regards.
edited Dec 1 '16 at 23:20
answered Dec 1 '16 at 23:13
MojtabaNava
306
306
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
|
show 1 more comment
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Please vote to my answer. :))
– MojtabaNava
Dec 4 '16 at 6:10
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
Thanks for your answer but unfortunately it didn't reply to my question.
– Fabio
Dec 4 '16 at 12:21
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You should use codes in your project . :))
– MojtabaNava
Dec 4 '16 at 12:54
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change fied UserId to companyId: step 1 to 7 .
– MojtabaNava
Dec 4 '16 at 12:56
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
You want change your table name : IdentityModels.cs
– MojtabaNava
Dec 4 '16 at 12:57
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40918199%2fhow-do-i-create-a-relationship-between-aspnetuserroles-and-some-other-table-usin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown