Identity icon indicating copy to clipboard operation
Identity copied to clipboard

Microsoft.AspNetCore.Identity Customize

Open pMonfared opened this issue 10 years ago • 5 comments

I want to customize Identity and override everything

Please See This : https://github.com/pMonfared/Asp.netCoreMultiLayer

and run project you will get this error :
An exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.ni.dll but was not handled in user code

Additional information: GenericArguments[0], 'SampleFive.DomainLayer.Models.ApplicationUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.

from this location in startup file :

 services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddUserStore<ApplicationUserStore>()
                .AddUserManager<ApplicationUserManager>()
                .AddRoleStore<ApplicationRoleManager>()
                .AddRoleManager<ApplicationRoleManager>()
                .AddEntityFrameworkStores<ApplicationDbContext, string>()
                .AddDefaultTokenProviders();

pMonfared avatar Aug 02 '16 19:08 pMonfared

See #855 , PR #893 & #918

verbatimt avatar Aug 02 '16 21:08 verbatimt

You also cannot use .AddEntityFrameworkStores<ApplicationDbContext, string>() when you customize everything, your calls to AddUserStore/RoleStore make that call unnecessary as well. so you should be able to remove that line.

HaoK avatar Aug 02 '16 21:08 HaoK

i use for Dependency Injection in my project with "StructureMap.Microsoft.DependencyInjection": "1.0.1" but when i config

private IServiceProvider IocConfig(IServiceCollection services)
        {
            var container = new Container();
            container.Configure(config =>
            {
                config.For<IConfigurationRoot>().Singleton().Use(() => Configuration);

                config.For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser,ApplicationRole,ApplicationDbContext,string,ApplicationUserClaim,ApplicationUserRole,ApplicationUserLogin,ApplicationUserToken>>();

                config.Scan(_ =>
                {
                    _.AssemblyContainingType<ISettingService>();
                    _.WithDefaultConventions();
                });
                config.For<IRoleStore<ApplicationRole>>().Use<ApplicationRoleStore>();
                config.For<IApplicationRoleStore>().Use<ApplicationRoleStore>();
                config.For<IApplicationUserStore>().Use<ApplicationUserStore>();
                config.For<IApplicationUserManager>().Use<ApplicationUserManager>();
                config.For<IApplicationRoleManager>().Use<ApplicationRoleManager>();
                config.For<IEmailSender>().Use<AuthMessageSender>();
                config.For<ISmsSender>().Use<AuthMessageSender>();

            });

            container.Populate(services);

            return container.GetInstance<IServiceProvider>();
        }

i have got error in this line

config.For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser,ApplicationRole,ApplicationDbContext,string,ApplicationUserClaim,ApplicationUserRole,ApplicationUserLogin,ApplicationUserToken>>();

error description:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0311  The type 'SampleFive.DomainLayer.Models.ApplicationRole' cannot be used as type parameter 'TRole' in the generic type or method 'UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken>'. There is no implicit reference conversion from 'SampleFive.DomainLayer.Models.ApplicationRole' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole<string, SampleFive.DomainLayer.Models.ApplicationUserRole, Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRoleClaim<string>>'.   SampleFive.Web..NETCoreApp,Version=v1.0 E:\Programming Projects\GitReps\CustomizeAspdotnetCore\SampleFive.Web\Startup.cs    157 Active

this is my ApplicationUser Class:

public class ApplicationUser : IdentityUser<string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin>
    {
        public ApplicationUser()
        {
            this.UserUsedPasswords = new List<ApplicationUserUsedPassword>();
        }
        public ICollection<ApplicationUserUsedPassword> UserUsedPasswords { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class ApplicationRole : IdentityRole<string, ApplicationUserRole, ApplicationRoleClaim>

    {
        public string Description { get; set; }
        public ApplicationRole()
        {
        }

        public ApplicationRole(string name)
            : this()
        {
            Name = name;
        }

        public ApplicationRole(string name, string description)
            : this(name)
        {
            this.Description = description;
        }

    }

    public class ApplicationUserLogin : IdentityUserLogin<string>
    {

    }
    public class ApplicationUserRole : IdentityUserRole<string>
    {

    }
    public class ApplicationRoleClaim : IdentityRoleClaim<string>
    {

    }
    public class ApplicationUserClaim : IdentityUserClaim<string>
    {

    }

    public class ApplicationUserToken : IdentityUserToken<string>
    {

    }

    public class ApplicationUserUsedPassword
    {
        public ApplicationUserUsedPassword()
        {
            CreatedDate = DateTimeOffset.UtcNow;
        }
        [Key]
        public int Id { get; set; }

        [Key, Column(Order = 0)]
        public string HashPassword { get; set; }
        public DateTimeOffset CreatedDate { get; set; }
        [Key, Column(Order = 1)]
        public string UserId { get; set; }
        public virtual ApplicationUser AppUser { get; set; }
    }

pMonfared avatar Aug 03 '16 02:08 pMonfared

Having same issues too, created this PR #923 to fix this.

furlanrapha avatar Aug 03 '16 13:08 furlanrapha

Dupe of https://github.com/aspnet/Identity/issues/855

HaoK avatar Aug 08 '16 20:08 HaoK