如何在没有角色的情况下使用ASP.NET Core Identity? [英] How to use ASP.NET Core Identity without roles?

查看:75
本文介绍了如何在没有角色的情况下使用ASP.NET Core Identity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有角色实现的情况下在asp.net core 2中实现标识是否可行?
我试图实现以下目标:

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

services.AddIdentityCore<TUser>();

但是这似乎不起作用.

推荐答案

我明白了!

我已经在github中上传了一个仓库: https://github.com/tolemac/IdentityWithoutRoles

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

您必须使用正确的DbSets创建您的自定义ApplicationDbContext:

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>>();

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

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

希望它会有用.

这篇关于如何在没有角色的情况下使用ASP.NET Core Identity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆