Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't inherit from IdentityUser and IdentityRole in ASP.NET Core 2.0

Tags:

c#

.net

asp.net

I'm trying to finish updating to .NET Core 2.0 but a couple of errors pop up:

The problem:

I have two classes, ApplicationRole and ApplicationUser which inherit some properties from IdentityRole and IdentityUser.

With the update to Core 2.0 I'm getting the following errors, claiming that IdentityRole and IdentityUser could not be found.

However, the package Microsoft.AspNetCore.Identity.EntityFrameworkCore 2.0 is installed in the new version of .NET Core

About IdentityRole:

Message 1:

The type or namespace name 'IdentityRole' could not be found (is it missing a using directive or an assembly reference?)

Message 2:

The 'Application.Models.ApplicationRole' type can not be used as a parameter of type 'TRole' in the generic type or method 'IdentityDbContext '. There is no implicit reference conversion from 'Application.Models.ApplicationRole' to 'Microsoft.AspNetCore.Identity.IdentityRole '.

Definition of ApplicationRole:

public class ApplicationRole:IdentityRole
{
    public string Description { get; set; }
    public DateTime CreatedDated { get; set; }
    public string IPAddress { get; set; }
}

About IdentityUser:

Message 1:

The type or namespace name 'IdentityUser' could not be found (is it missing a using directive or an assembly reference?)

Message 2:

The 'Application.Models.ApplicationUser' type can not be used as a parameter of type 'TUser' in the generic type or method 'IdentityDbContext '. There is no implicit reference conversion from 'Application.Models.ApplicationUser' to 'Microsoft.AspNetCore.Identity.IdentityUser '.

Definition of ApplicationUser

public class ApplicationUser:IdentityUser
{
    public string Name { get; set; }
}

The guide followed to define this classes and their use are here:

http://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/

With the change to Core 2.0 I wonder how IdentityRole and IdentityUser changed so I can no longer inherit from them.

like image 952
Luis Alberto Delgado de la Flo Avatar asked Aug 23 '17 04:08

Luis Alberto Delgado de la Flo


3 Answers

Solved.

The package that was keeping those classes Microsoft.AspNetCore.Identity.EntityFrameworkCore changed. To access those clases (IdentityUser and IdentityRole) one must add

using Microsoft.AspNetCore.Identity;

With this, the problem is gone.

like image 79
Luis Alberto Delgado de la Flo Avatar answered Oct 13 '22 20:10

Luis Alberto Delgado de la Flo


ICollection<IdentityUserRole<int>> Roles , ICollection<IdentityUserClaim<int>> Claims and ICollection<IdentityUserLogin<int>> Logins navigation properties have been removed from Microsoft.AspNetCore.Identity.IdentityUser .

You should define them manually

public class MyUser : IdentityUser
{                
    public virtual ICollection<IdentityUserRole<int>> Roles { get; } = new List<IdentityUserRole<int>>();

    public virtual ICollection<IdentityUserClaim<int>> Claims { get; } = new List<IdentityUserClaim<int>>();

    public virtual ICollection<IdentityUserLogin<int>> Logins { get; } = new List<IdentityUserLogin<int>>();
}

To prevent duplicate foreign keys when running EF Core Migrations, add the following to your IdentityDbContext class' OnModelCreating method

protected override void OnModelCreating(ModelBuilder builder)
{
  base.OnModelCreating(builder);

builder.Entity<MyUser>()
    .HasMany(e => e.Claims)
    .WithOne()
    .HasForeignKey(e => e.UserId)
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);

builder.Entity<MyUser>()
    .HasMany(e => e.Logins)
    .WithOne()
    .HasForeignKey(e => e.UserId)
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);

builder.Entity<MyUser>()
    .HasMany(e => e.Roles)
    .WithOne()
    .HasForeignKey(e => e.UserId)
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);
}

Migrating Authentication and Identity to ASP.NET Core 2.0

like image 8
tchelidze Avatar answered Oct 13 '22 19:10

tchelidze


For me it got solved by installing package:

Microsoft.Extensions.Identity.Stores
like image 7
Alexander Burias Avatar answered Oct 13 '22 20:10

Alexander Burias