【问题标题】:How to use ASP.NET Core Identity without roles?如何在没有角色的情况下使用 ASP.NET Core Identity?
【发布时间】:2018-08-03 07:15:21
【问题描述】:

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

services.AddIdentityCore<TUser>();

但这似乎并不奏效。

【问题讨论】:

  • 如果您不需要角色,则不必使用它们。撕掉它们会比你得到的任何好处带来更多的痛苦/工作。
  • ASP.NET Core 身份框架也可以只提供用户管理而无需角色。我猜你正在寻找用户名和密码之类的东西。让身份数据库拥有所有表,然后只使用用户表
  • 而不是以下内容:services.AddIdentity 撕掉角色是否可行?
  • @MithunPattankar 我也不想使用 EF。
  • 我正在尝试使用 AddIdentityCore,但我遇到了问题。当我登录时一切都很好,但 User.Identity.IsAuthenticated 总是错误的。

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


【解决方案1】:

我明白了!

我已经在github上传了一个repo: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

希望对你有用。

【讨论】:

  • UserManager 代替 UserStore 怎么样?
猜你喜欢
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2018-01-08
  • 1970-01-01
相关资源
最近更新 更多