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?
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?
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
Don't forget to add migrations and update the database. Otherwise it throws a dependecy injection exceptions for the identity.