This project has moved and is read-only. For the latest updates, please go here.
1
Vote

'ElCamino.AspNet.Identity.AzureTable.UserStore`3[TUser,TRole,TContext]' violates the constraint of type 'TUser'.

description

I think I found an issue implementing Roles in an ASPNET 5.0 (DNX 4.5.1) application with WindowsAzure.Storage 6.2.2-preview and ElCamino.AspNet.Identity.AzureTable 2.0.0-beta2. I had trouble getting it to work. I'm not sure if I am doing something wrong or it is a bug in the beta2.

I kept running into the error:
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Additional information: GenericArguments[0], 'Web.Model.ApplicationUser', on 'ElCamino.AspNet.Identity.AzureTable.UserStore`3[TUser,TRole,TContext]' violates the constraint of type 'TUser'.

This is the code I was using:
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
    {      
        public string FirstName { get; set; }
    }

    public class ApplicationUserRole : IdentityUserRole<string>
    {

    }

    public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
    {

    }
    
    public class ApplicationDbContext : IdentityCloudContext
    {
        public ApplicationDbContext() : base() { }

        public ApplicationDbContext(IdentityConfiguration config) : base(config) { }
    }
In Startup.cs
           services
                .AddIdentity<ApplicationUser, ApplicationRole>()
                .AddAzureTableStores<ApplicationDbContext>(() =>
               new IdentityConfiguration()
               {
                   StorageConnectionString = Configuration.Get<string>("AppSettings:StorageConnectionString")
               })
                .AddDefaultTokenProviders()
                .CreateAzureTablesIfNotExists<ApplicationDbContext>(); 
The error comes from the file: IdentityAzureTableBuilderExtensions.cs, and the line:
Type userStoreType = typeof(UserStore<,,>).MakeGenericType(builder.UserType, builder.RoleType, contextType);
This code is specific for #if !net45

So if I change my code:
    public class ApplicationUser : IdentityUser
    {      
        public ApplicationUser() : base() { }

        public string FirstName { get; set; }
    }
    
    public class ApplicationRole : IdentityRole
    {
        public ApplicationRole() : base() { }
    }
And add:
    public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext>
    {
        public ApplicationUserStore(ApplicationDbContext context)
            : base(context)
        { }
    }
Note the only 3 generic arguments, instead of 1 or 6. Then it works.

So maybe this is a bug or just restriction when you don't run .NET 45. It is not totally clear to me. Just figured I'd report it.

comments