如何在没有角色的情况下使用ASP.NET Core Identity?

时间:2018-02-23 08:59:42

标签: c# asp.net-core asp.net-core-2.0 asp.net-core-identity

在没有角色实现的情况下在asp.net核心2中实现身份是否可行? 我试图实现以下内容:

services.AddIdentityCore<TUser>();

但这似乎也没有效果。

1 个答案:

答案 0 :(得分:3)

我明白了!

我在github上传了一个回购: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>>();

您必须手动使用AddIdentityCoreAddUserStoreAddAuthentication,还必须配置ISecurityStampValidator

希望它会有用。