I am having trouble with switching partially to .NET Standard.
I am in the process of migrating a class library to .NET Standard, in this library I have the repositories and the database communication. I already migrated this successfully to use AspNetCore.Identity.EntityFrameworkCore
. What I try to achieve is eventually having 1 .NET Standard project taking care of the database, where 1 MVC .NET Framework, 1 API .NET Framework and 1 new .NET Core application will be using it. Besides that a few other .NET Framework class libraries depend on it. Basically the .NET Core application has been made already but the back-end has not been 'merged' for overlapping functionalities.
Little overview:
Reason for not converting MVC/API to .Core is that too many other libraries currently depend on .NET Framework, some are not yet convertible, but using the same library for the database is a fundamental change which will avoid double implementations of some repositories.
I have also already converted my entities that implement Microsoft.AspNetCore.Identity.IdentityUser
, Microsoft.AspNetCore.Identity.IdentityRole
, etc.
So my DbContext class looks like this:
public class DatabaseContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
{
private IConfiguration _config;
public DatabaseContext(IConfiguration config) : base()
{
_config = config;
}
//all DbSet, OnModelCreating
}
I have successfully ran the EFCore Code First Migrations.
Now I am trying to configure the Identity in my MVC application (and then in the API project as well).
I just have the standard IdentityConfig.cs
, Startup.Auth.cs
where all the configuration is done. I have tried looking at this documentation (migration identity). All I could do is add this, where AddMvc()
does not exist so that throws an compile error:
Startup.cs
using System;
using System.IO;
using Babywatcher.Core.Data.Database;
using Babywatcher.Core.Data.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(MyProject.MVC.Startup))]
namespace MyProject.MVC
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var services = new ServiceCollection();
ConfigureAuth(app);
ConfigureServices(services);
}
public void ConfigureServices(IServiceCollection services)
{
// Add EF services to the services container.
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(""));//Configuration trying to refer to above method: Configuration.GetConnectionString("DefaultConnection")
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<DatabaseContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
}
}
Well I guess this doesn't really do much, Also I am using SimpleInjector throughout both .NET Framework projects which I would prefer to keep using if possible in stead of going to use the default dependency injector.
By adding above I don't really know what to do with the ConfigureAuth
method and where to put all the configuration.
When I try to adjust the IdentityManager, to try and reference the same types within AspNetCore.Identity
I start to get issues when trying to change the ApplicationUserManager
:
Creating the UserStore is not a problem, but trying to create the UserManager
is more difficult, I also have tried to use this in my UserRepository
, like I did before, which I can't use now anymore :(.
Old UserRepository
private readonly UserManager<ApplicationUser, string> _userManager = null;
private readonly RoleManager<ApplicationRole, string> _roleManager = null;
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public UserRepository(DatabaseContext dbContext) : base(dbContext, c => c.contactId, m => m.ContactId)
{
var userStore =
new UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(dbContext);
var roleStore = new RoleStore<ApplicationRole, string, ApplicationUserRole>(dbContext);
_userManager = new UserManager<ApplicationUser, string>(userStore);
_roleManager = new RoleManager<ApplicationRole, string>(roleStore);
_userManager.UserValidator = new UserValidator<ApplicationUser>(_userManager) { AllowOnlyAlphanumericUserNames = false };
if (DataProtectionProvider == null)
{
DataProtectionProvider = new MachineKeyProtectionProvider();
}
_userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, string>(DataProtectionProvider.Create("Identity"));
}
Attempting to do above again gives issues when creating the UserManager
because of the 9 arguments it asks so I kind-of felt that this shouldn't be done this way... Probably going to do something like this to get them there of course I first want to fix the root problem.
Last but not least: Keep in mind that these applications are all in production already, so especially around the users I need to make sure that the logins all still work. When bringing this version live I will need to migrate the data to a new database because of some other migration issues.