Reputation: 73908
I need to integrate Asp.Net latest MVC version with an existing database which has an additional column String Address
to table dbo.AspNetUsers
I need to create an instance ApplicationUser
which has property Address.
Any idea how to do it?
Upvotes: 62
Views: 102998
Reputation: 1
Don't forget to add migrations and update the database. Otherwise it throws a dependecy injection exceptions for the identity.
Upvotes: 0
Reputation: 189
I had recently the same problem. I had an apllication created with DBFirst aproach and I needed to add Identity. This is what I did.
1. Microsoft.EntityFrameworkCore 2. Microsoft.EntityFrameworkCore.Design 3. Microsoft.EntityFrameworkCore.SqlServer 4. Microsoft.AspNetCore.Identity 5. Microsoft.AspNetCore.Identity.EntityFrameworkCore 6. Microsoft.AspNetCore.Aututhentication.JwtBearer
public partial class BookStoresDBContext : IdentityDbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
}
As far as it was a created project the StringConnection was already there, if not add it.
On the Startup.cs configure Identity service on ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BookStoresDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BookStoreDB")));
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequiredLength = 5;
}).AddEntityFrameworkStores<BookStoresDBContext>()
.AddDefaultTokenProviders();
}
You can configure the Authetication service too
services.AddAuthentication(auth =>
{
auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
auth.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => {
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Your key to encrypt"))
};
});
Then run the migration from the Package Manager Console
Add-Migration InitDb
On the migration file, remove all the migrationBuilder.CreateTable for the tables you already have in your Database
Update the Database from the Package Manager Console
Update-Database
I hope it result usefull 😁
Upvotes: 2
Reputation: 73908
A possible solution which works for me, basically I am able to integrate Asp.Net Identity User Profiles with an existing Database.
Getting the Asp.Identity Tables:
Alternatively use something like http://identity.codeplex.com/
Integrating with your existing db:
Now you have the Asp.Identity Tables in your db with ER model in your application.
Asp.Identity Profile Adding new properties:
Execute the command “Enable-Migrations”; Once we enabled the database migrations, we can go ahead and add new properties for our UserProfile
To Add new properties modify IdentityModels.cs file, example:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailID { get; set; }
}
Add New Migration
Once we added the properties, bring the Package Manager Console and execute the following command.
Add-Migration “YouMigrationName”
This command will generate a database script file, now execute following command to run this script file against the database.
Update-Database
Now, all the new properties will turn into table fields in the same database table.
I hope it can help others, if you have a better idea please let me know.
Upvotes: 59
Reputation: 1
public class MyUser : IdentityUser { public virtual MyUserInfo MyUserInfo { get; set; } }
public class MyUserInfo{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class MyDbContext : IdentityDbContext<MyUserInfo> //Edited to MyUserInfo
{
public MyDbContext()
: base("DefaultConnection")
{
}
public System.Data.Entity.DbSet<MyUserInfo> MyUserInfo { get; set; }
}
Getting Profile information
When the User Logs in, you can display the profile information by doing the following
Get the current logged in UserId
, so you can look the user up in ASP.NET Identity system
var currentUserId = User.Identity.GetUserId();
Instantiate the UserManager
in ASP.Identity system so you can look up the user in the system
var manager = new UserManager<MyUser>(new UserStore<MyUser>(new MyDbContext()));
Get the User object
var currentUser = manager.FindById(User.Identity.GetUserId());
Get the profile information about the user
currentUser.MyUserInfo.FirstName
Upvotes: -3
Reputation: 38358
Take a look at these projects on GitHub:
Which includes:
Upvotes: 20