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

编程入门 行业动态 更新时间:2024-10-09 05:25:17
本文介绍了如何在没有角色的情况下使用ASP.NET Core Identity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

services.AddIdentityCore<TUser>();

但是这似乎不起作用.

推荐答案

我明白了!

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

I have upload a repo in github: github/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>>();

您必须手动使用AddIdentityCore,AddUserStore和AddAuthentication,也必须配置ISecurityStampValidator.

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

希望它会有用.

更多推荐

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

本文发布于:2023-11-07 15:42:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   角色   如何在   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!