我得到了它!
我已经在github上传了一个repo:https://github.com/tolemac/IdentityWithoutRoles https://github.com/tolemac/IdentityWithoutRoles
您必须创建您的自定义ApplicationDbContext
有更正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.
}
}
然后你必须这样配置服务:
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>>();
你必须使用AddIdentityCore
, AddUserStore
and AddAuthentication
手动,必须配置ISecurityStampValidator
too.
希望它会有用。