获取 .NET Core 2.1 Identity 用户和关联角色
在 .NET Core Identity 中,检索用户及其关联角色可能具有挑战性。虽然早期版本的 Identity 在 ApplicationUser 中提供了内置 Roles 属性,但此功能已不再存在。
解决方案
要解决此问题,请实施以下操作解决方案:
ApplicationUser
public class ApplicationUser : IdentityUser { public ICollection<applicationuserrole> UserRoles { get; set; } }</applicationuserrole>
ApplicationUserRol e
public class ApplicationUserRole : IdentityUserRole<string> { public virtual ApplicationUser User { get; set; } public virtual ApplicationRole Role { get; set; } }</string>
ApplicationRole
public class ApplicationRole : IdentityRole { public ICollection<applicationuserrole> UserRoles { get; set; } }</applicationuserrole>
ApplicationDbContext (添加关系,配置种子)
public class ApplicationDbContext : IdentityDbContext<applicationuser applicationrole string identityuserclaim>, ApplicationUserRole, IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>> { public DbSet<applicationuserrole> UserRoles { get; set; } // migrations omitted for brevity }</applicationuserrole></string></string></string></applicationuser>
启动
services.AddIdentity<applicationuser applicationrole>(options => options.Stores.MaxLengthForKeys = 128) .AddEntityFrameworkStores<applicationdbcontext>() .AddDefaultTokenProviders();</applicationdbcontext></applicationuser>
Razor 页面代码(立即加载相关数据)
this.Users = userManager.Users.Include(u => u.UserRoles).ThenInclude(ur => ur.Role).ToList();
ASP 核心 2.2更新
对于 ASP Core 2.2 及更高版本,固有于 IdentityUserRole
我尝试遵循建议的解决方案,但导致错误:“‘field’中的未知列‘u.Roles.ApplicationUserId’列表''。为了解决这个问题,我研究了 GitHub 评论/问题并实施了上述解决方案。关键修改涉及创建新的 ApplicationUserRole 和 ApplicationRole 类、更新 ApplicationUser 类以反映新关系,以及向 DbContext 中的模型构建器添加自定义配置。通过预先加载用户的 UserRoles,然后加载 UserRole 的角色,我能够成功检索用户及其关联的角色。
以上是如何高效检索.NET Core 2.1身份用户及其关联角色?的详细内容。更多信息请关注PHP中文网其他相关文章!

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'

MySQL中的字符串数据类型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序规则(Collations)决定了字符串的比较和排序方式。1.CHAR适合固定长度字符串,VARCHAR适合可变长度字符串。2.BINARY和VARBINARY用于二进制数据,BLOB和TEXT用于大对象数据。3.排序规则如utf8mb4_unicode_ci忽略大小写,适合用户名;utf8mb4_bin区分大小写,适合需要精确比较的字段。

最佳的MySQLVARCHAR列长度选择应基于数据分析、考虑未来增长、评估性能影响及字符集需求。1)分析数据以确定典型长度;2)预留未来扩展空间;3)注意大长度对性能的影响;4)考虑字符集对存储的影响。通过这些步骤,可以优化数据库的效率和扩展性。

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中间布洛布(16,777,215个比例),andlongblob(4,294,967,967,295 bytes).tousebl观察性:1)考虑performance impactsandSandStorelargeblobsextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

自动化在MySQL中创建用户的最佳工具和技术包括:1.MySQLWorkbench,适用于小型到中型环境,易于使用但资源消耗大;2.Ansible,适用于多服务器环境,简单但学习曲线陡峭;3.自定义Python脚本,灵活但需确保脚本安全性;4.Puppet和Chef,适用于大规模环境,复杂但可扩展。选择时需考虑规模、学习曲线和集成需求。

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)转换theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchusiseLike.2)forCompresseBlyblobs,useuncompresseblobs,useuncompressbeforeconversion.3)acpperformance impperformance imperformance imptactsanddataEccoding.4)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版
中文版,非常好用

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中