Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ASP.NET Core Identity without roles?

Is it feasible to implement identity in asp.net core 2 without roles implementation?
I have tried to implement the following:

services.AddIdentityCore<TUser>();

but that does not seem to work as well.

like image 364
Hardik Fefar Avatar asked Feb 23 '18 08:02

Hardik Fefar


1 Answers

I got it!

I have upload a repo in github: https://github.com/tolemac/IdentityWithoutRoles

You have to create your custom ApplicationDbContext with corrects DbSets:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions options) : base(options)
    {
    }

    /// <summary>
    /// Gets or sets the <see cref="DbSet{TEntity}"/> of Users.
    /// </summary>
    public DbSet<ApplicationUser> WebUsers { get; set; }

    /// <summary>
    /// Gets or sets the <see cref="DbSet{TEntity}"/> of User claims.
    /// </summary>
    public DbSet<IdentityUserClaim<long>> UserClaims { get; set; }

    /// <summary>
    /// Gets or sets the <see cref="DbSet{TEntity}"/> of User logins.
    /// </summary>
    public DbSet<IdentityUserLogin<long>> UserLogins { get; set; }

    /// <summary>
    /// Gets or sets the <see cref="DbSet{TEntity}"/> of User tokens.
    /// </summary>
    public DbSet<IdentityUserToken<long>> UserTokens { get; set; }

    /// <summary>
    /// Configures the schema needed for the identity framework.
    /// </summary>
    /// <param name="builder">
    /// The builder being used to construct the model for this context.
    /// </param>
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        // add your model builder code here.
    }
}

Then you have to configure the services this way:

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseInMemoryDatabase("WebApplicationTestingDatabase"));//.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentityCore<ApplicationUser>(options =>
            {
                // Identity options configuration
                options.Password.RequireDigit = true;
                options.Password.RequiredLength = 8;
                options.Password.RequireNonAlphanumeric = true;
                options.Password.RequireUppercase = true;
                options.Password.RequireLowercase = true;
            })
            .AddUserStore<UserStore<ApplicationUser, IdentityRole<long>, ApplicationDbContext, long>>()
            .AddDefaultTokenProviders()
            .AddSignInManager<SignInManager<ApplicationUser>>();

        services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
                options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
                options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
            }).AddCookie(IdentityConstants.ApplicationScheme, o =>
            {
                o.LoginPath = new PathString("/Account/Login");
                o.Events = new CookieAuthenticationEvents()
                {
                    OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
                };
            }).AddCookie(IdentityConstants.ExternalScheme, o =>
            {
                o.Cookie.Name = IdentityConstants.ExternalScheme;
                o.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
            })
            .AddCookie(IdentityConstants.TwoFactorRememberMeScheme,
                o => o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme)
            .AddCookie(IdentityConstants.TwoFactorUserIdScheme,
                o =>
                {
                    o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
                    o.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
                }
            );
        services.AddScoped<ISecurityStampValidator, SecurityStampValidator<ApplicationUser>>();

You have to use AddIdentityCore, AddUserStore and AddAuthentication manually, have to configure ISecurityStampValidator too.

Hope it will be useful.

like image 99
Javier Ros Avatar answered Sep 22 '22 15:09

Javier Ros