asp.net-identity 相关问题

ASP.NET Identity系统旨在取代以前的ASP.NET成员资格和简单成员资格系统。

C# 我可以从 EntityFramework 扩展 IdentityMessage 吗?

(使用 Microsoft.AspNet.Identity) EmailService 中的 IdentityMessage 消息有 3 个属性 公共虚拟字符串主体 { 得到;放; } 公共虚拟字符串目的地 { 得到;放; }

回答 2 投票 0

我们能否在 duende 身份服务器中支持不同的密码哈希算法

当前,当我将用户添加到系统时,我正在使用 Microsoft.AspNetCore.Identity.PasswordHasher 进行密码哈希处理,并且效果很好。但是为了改变这种方法并使用

回答 1 投票 0

HttpContext.User 在任何控制器中始终为 null

我在任何控制器中作为用户总是空的 var usr = 用户作为 ClaimsPrincipal; 我以这种方式配置了我的应用程序: var builder = WebApplication.CreateBuilder(args); ConfigurationManager 配置...

回答 1 投票 0

实时服务器中的 ASP.NET Core 身份超时

在我的本地 IIS 中,我的网站永远不会超时。在我的生产环境中,超时似乎是 5 分钟左右,然后我立即进入登录页面。 我尝试了以下没有影响:

回答 2 投票 0

使用身份用户登录时出现问题,我在 PasswordSignInAsync 上遇到死锁?

我使用 .net core 7 开发 blazor 服务器 Web 应用程序。使用用户身份登录时我遇到问题死锁 var asignin = signInManager.PasswordSignInAsync(userDto.UserName, "Coding@1234?", f...

回答 1 投票 0

虽然我申请了相同的密码,但比较C#的哈希密码和用户身份的哈希密码不一样?

我在 .NET Core 7 和 VS 2022 上以用户身份使用 Blazor Server Web 应用程序工作。 我在比较用户身份 hasher.HashPassword 和 userManager.CreateAsync 的密码时遇到问题。 我不明白...

回答 1 投票 0

.NET 7 Controller Authorization with Identity and JWT

这是这篇文章的后续: .NET 7 Identity JwtBearer - 授权属性不影响 API 我有一个与 React.ts 通信的 .NET web api,我正在尝试添加基于角色的

回答 0 投票 0

为什么我在使用Identity登录asp.net core时出现“无法为'IdentityUserClaim<string>'创建DbSet”的错误?

我正在使用“AddDefaultIdentity”的“AddIdentityCore”,这是我的代码 builder.Services.AddIdentityCore(选项=> { 选项.登录.

回答 0 投票 0

“未指定 authenticationScheme,也没有找到 DefaultChallengeScheme。”在没有 cookie 的情况下发送请求时

我正在使用基于 cookie 的身份验证制作 webapp。我有登录屏幕,后端发送 cookie,然后浏览器随每个请求发送 cookie。 但是当我尝试到达安全端点时(使用或无线...

回答 0 投票 0

ASP.NET Core 在客户端 Web 应用程序上为现有令牌服务配置身份验证?

我开发了一个 API,其中包含一个获取令牌的端点,它充当令牌服务和资源服务器。它使用 ASP.NET 标识。一切正常,但是我现在想实施一个

回答 1 投票 0

实体框架 6.0,身份,一对多关系在 IdentityUser 类中不起作用

我对与身份和实体框架相关的问题感到疯狂。我正在使用代码优先的方法。 我有这些课程: 公共类 WebApplicationUser:IdentityUser { 公共虚拟

回答 1 投票 0

ASP.NET WebApi 返回 HTTP 404 而不是 HTTP 401

来自 Program.cs 的简单脚手架个人身份验证,: var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??抛出新的 InvalidOperationExcep ...

回答 1 投票 0

将 sql 查询限制为当前登录的用户

我的技术堆栈是带有 Azure AD (B2C) 身份验证的 ASP.NET Core。我使用 EF Core 和 Azure SQL 来存储数据。我有一个包含来自多个用户的数据的表,例如来自在线商店的订单,其中

回答 0 投票 0

System.InvalidOperationException:无法解析“Microsoft.AspNetCore.Identity.SignInManager`1

我无法登录我的应用程序。我正在使用邮递员使用用户名和密码向我的身份验证控制器发布请求,但是我收到以下错误消息。我最近从 .net 5 升级 ...

回答 1 投票 0

为 Asp.Net Identity 启用迁移

嗨,如何在 Asp.Net 身份应用程序 DbContext 中启用迁移? 我有一些表的项目。并在其上安装来自 Nuget 的身份。 我的表与身份用户表相关。 我想...

回答 1 投票 0

如何创建具有entityframeworkcode身份的不同类型的用户?

示例:我有 3 种类型的用户,如 x、y、z,X 具有 a、b、c 的属性。 Y 具有 a,b,c d,e 的属性,Z 具有 f,g,h 的属性。 这三类用户可能有不同类型的索赔......

回答 0 投票 0

使用 EF 6 将用户添加到 ASP.Net Identity 中的角色会导致创建新角色和用户

我在这方面碰壁了,希望其他人有答案,我创建了一个带有身份的 Blazor 应用程序,这就是我的 dbContext 的样子: 公共类 ApplicationDbContext : IdentityDbContext 我在这方面遇到了困难,希望其他人有答案,我创建了一个带有身份的 Blazor 应用程序,这就是我的dbContext 的样子: public class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, string, AppUserClaim,AppUserRole,AppUserLogin,AppRoleClaim,AppUserToken> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<AppUser>().ToTable("Users"); builder.Entity<AppUser>().Property(x => x.FirstName).IsRequired(); builder.Entity<AppUser>().Property(x => x.LastName).IsRequired(); builder.Entity<AppUser>().Property(x => x.LastLoginDate); builder.Entity<AppUser>().HasMany(x => x.Roles).WithOne(x=>x.User).HasForeignKey(x=>x.UserId).IsRequired(); builder.Entity<AppUserRole>().ToTable("UserRoles").HasOne(c => c.Role).WithMany(c => c.UserRoles).HasForeignKey(c => c.UserId); builder.Entity<AppRole>().ToTable("Roles"); builder.Entity<AppRole>().Property(x => x.Description); builder.Entity<AppRole>().HasMany(x => x.UserRoles).WithOne(e => e.Role).HasForeignKey(ur => ur.RoleId).IsRequired(); builder.Entity<AppRole>().HasMany(x => x.RoleClaims).WithOne(e=>e.Role).HasForeignKey(x=>x.RoleId).IsRequired(); builder.Entity<AppRoleClaim>().ToTable("RoleClaims"); builder.Entity<AppRoleClaim>().HasOne(x => x.Role); builder.Entity<AppUserClaim>().ToTable("UserClaims"); } } 在我的program.cs中,我有这个代码: builder.Services.AddIdentity<AppUser, AppRole>() .AddEntityFrameworkStores<ApplicationDbContext>(); builder.Services.AddTransient<RoleManager<AppRole>>(); 这是我的身份类: public class AppRole:IdentityRole { public string? Description { get; set; } public virtual ICollection<AppUserRole>? UserRoles { get; set; } public virtual ICollection<AppRoleClaim>? RoleClaims { get; set; } public AppRole() { } } public class AppRoleClaim : IdentityRoleClaim<string> { public virtual AppRole Role { get; set; } public AppRoleClaim() { Role = new AppRole(); } } public class AppUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public DateTimeOffset? LastLoginDate { get; set; } public byte[]? Photo { get; set; } public virtual ICollection<AppUserRole> Roles { get; set; } public AppUser() { FirstName = ""; LastName = ""; Roles = new List<AppUserRole>(); } } public class AppUserClaim : IdentityUserClaim<string> { } public class AppUserLogin : IdentityUserLogin<string> { } public class AppUserRole : IdentityUserRole<string> { public virtual AppUser User { get; set; } public virtual AppRole Role { get; set; } public AppUserRole() { User = new AppUser(); Role = new AppRole(); } } public class AppUserToken : IdentityUserToken<string> { } 这是我的表结构: 最后我的问题是,当我调用 UserManager.CreateAsync(NewUser) 时,一个用户被正确创建,但是当我尝试将新创建的用户添加到一个角色时,例如像这样的管理员角色 await UserManager.AddToRoleAsync(NewUser, "Admin"); 它不会抛出任何错误,但是什么碰巧它创建了一个具有空用户名、名字等的新用户,并向角色表中添加了一个具有空名称和空规范化名称的新角色。然后将该角色添加到UserRoles 表中。一切都没有任何错误。 根据 MS 文档,这应该创建一个新用户并将该用户添加到管理员角色。我从中构建的文档在这里https://learn.microsoft.com/en-us/aspnet/core/security/?view=aspnetcore-6.0 今天做了一些挖掘之后,我弄清楚了是怎么回事。看看我的AppUserRole课 public class AppUserRole : IdentityUserRole<string> { public virtual AppUser User { get; set; } public virtual AppRole Role { get; set; } public AppUserRole() { User = new AppUser(); Role = new AppRole(); } } 因为我正在新建一个新的 AppUser() 和一个新的 AppRole() 他们依次调用基类构造函数,其中 IdentityRole 将其 Id 属性设置为新的 Guid 当构造函数时发生同样的事情IdentityUser 被称为。我通过查看 AspNetCore.Identity 源代码发现了这一点。所以 EntityFramework 正确地完成了它的工作,它看到了一个全新的 AppUserRole 和一个新的 IdentityUser 和新的 IdentityRole,因为这些是 AppRole 的导航属性,所以它在数据库中创建了它们。 TLDR 解决方法是使 AppUserRole 类中的导航属性可为空,而不是在构造函数中新建它们,如下所示: public class AppUserRole : IdentityUserRole<string> { public virtual AppUser? User { get; set; } public virtual AppRole? Role { get; set; } public AppUserRole() { } }

回答 1 投票 0

验证服务描述符 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory`1

第一次尝试添加迁移时出现此错误。我添加了扩展方法和我的服务类 public void ConfigureServices(IServiceCollection 服务) { 服务...

回答 3 投票 0

ASP.NET Core 身份与 Blazor 和实体框架 6

我在这方面碰壁了,希望其他人有答案,我创建了一个带有身份的 Blazor 应用程序,这就是我的 dbContext 的样子: 公共类 ApplicationDbContext : IdentityDbContext 我在这方面遇到了困难,希望其他人有答案,我创建了一个带有身份的 Blazor 应用程序,这就是我的dbContext 的样子: public class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, string, AppUserClaim,AppUserRole,AppUserLogin,AppRoleClaim,AppUserToken> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<AppUser>().ToTable("Users"); builder.Entity<AppUser>().Property(x => x.FirstName).IsRequired(); builder.Entity<AppUser>().Property(x => x.LastName).IsRequired(); builder.Entity<AppUser>().Property(x => x.LastLoginDate); builder.Entity<AppUser>().HasMany(x => x.Roles).WithOne(x=>x.User).HasForeignKey(x=>x.UserId).IsRequired(); builder.Entity<AppUserRole>().ToTable("UserRoles").HasOne(c => c.Role).WithMany(c => c.UserRoles).HasForeignKey(c => c.UserId); builder.Entity<AppRole>().ToTable("Roles"); builder.Entity<AppRole>().Property(x => x.Description); builder.Entity<AppRole>().HasMany(x => x.UserRoles).WithOne(e => e.Role).HasForeignKey(ur => ur.RoleId).IsRequired(); builder.Entity<AppRole>().HasMany(x => x.RoleClaims).WithOne(e=>e.Role).HasForeignKey(x=>x.RoleId).IsRequired(); builder.Entity<AppRoleClaim>().ToTable("RoleClaims"); builder.Entity<AppRoleClaim>().HasOne(x => x.Role); builder.Entity<AppUserClaim>().ToTable("UserClaims"); } } 在我的program.cs中,我有这个代码: builder.Services.AddIdentity<AppUser, AppRole>() .AddEntityFrameworkStores<ApplicationDbContext>(); builder.Services.AddTransient<RoleManager<AppRole>>(); 这是我的身份类: public class AppRole:IdentityRole { public string? Description { get; set; } public virtual ICollection<AppUserRole>? UserRoles { get; set; } public virtual ICollection<AppRoleClaim>? RoleClaims { get; set; } public AppRole() { } } public class AppRoleClaim : IdentityRoleClaim<string> { public virtual AppRole Role { get; set; } public AppRoleClaim() { Role = new AppRole(); } } public class AppUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public DateTimeOffset? LastLoginDate { get; set; } public byte[]? Photo { get; set; } public virtual ICollection<AppUserRole> Roles { get; set; } public AppUser() { FirstName = ""; LastName = ""; Roles = new List<AppUserRole>(); } } public class AppUserClaim : IdentityUserClaim<string> { } public class AppUserLogin : IdentityUserLogin<string> { } public class AppUserRole : IdentityUserRole<string> { public virtual AppUser User { get; set; } public virtual AppRole Role { get; set; } public AppUserRole() { User = new AppUser(); Role = new AppRole(); } } public class AppUserToken : IdentityUserToken<string> { } 这是我的表结构: 最后我的问题是,当我调用 UserManager.CreateAsync(NewUser) 时,一个用户被正确创建,但是当我尝试将新创建的用户添加到一个角色时,例如像这样的管理员角色 await UserManager.AddToRoleAsync(NewUser, "Admin"); 它不会抛出任何错误,但是什么碰巧它创建了一个具有空用户名、名字等的新用户,并向角色表中添加了一个具有空名称和空规范化名称的新角色。然后将该角色添加到UserRoles 表中。一切都没有任何错误。 根据 MS 文档,这应该创建一个新用户并将该用户添加到管理员角色。我从中构建的文档在这里https://learn.microsoft.com/en-us/aspnet/core/security/?view=aspnetcore-6.0 今天做了一些挖掘之后,我弄清楚了是怎么回事。看看我的AppUserRole课 public class AppUserRole : IdentityUserRole<string> { public virtual AppUser User { get; set; } public virtual AppRole Role { get; set; } public AppUserRole() { User = new AppUser(); Role = new AppRole(); } } 因为我正在新建一个新的 AppUser() 和一个新的 AppRole() 他们依次调用基类构造函数,其中 IdentityRole 将其 Id 属性设置为新的 Guid 当构造函数时发生同样的事情IdentityUser 被称为。我通过查看 AspNetCore.Identity 源代码发现了这一点。所以 EntityFramework 正确地完成了它的工作,它看到了一个全新的 AppUserRole 和一个新的 IdentityUser 和新的 IdentityRole,因为这些是 AppRole 的导航属性,所以它在数据库中创建了它们。 TLDR 解决方法是使 AppUserRole 类中的导航属性可为空,而不是在构造函数中新建它们,如下所示: public class AppUserRole : IdentityUserRole<string> { public virtual AppUser? User { get; set; } public virtual AppRole? Role { get; set; } public AppUserRole() { } }

回答 1 投票 0

ASP.NET 身份检查用户角色不起作用

我有一个 ASP.NET MVC 5 应用程序。我使用标准的 ASP.NET 身份提供者进行用户和角色管理。重要的是我使用自己的存储库项目中的 IdentityUser,但是

回答 6 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.