- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
...我们只在 Microsoft.AspNet.Identity 内部。 (我们甚至没有查看 Microsoft.AspNet.Identity.EntityFramework 的基本实现。)
UserManager
类只接受 IUserStore<TUser>
在构造函数中。它没有 IUserRoleStore<TUserRole>
我想需要访问它来确定是否 UserManager.IsInRoleAsync(string, string)
或不。
我认为 UserStore 的实现会有一个 IsInRoleAsync(string, string)
也能发挥作用(那么一切就有意义了),但事实并非如此。
另一个奇怪的事情 - 如果 UserManager 在其实现中只知道我们正在处理 IUser
,它如何能够执行密码设置和重置? - 仅具有 string Id
和string UserName
作为属性?
最佳答案
好吧,经过大量的挖掘和幸运的发现 - 事实证明 Microsoft.AspNet.Identity.Core 附带了一些其他接口(interface),特别是 IUserRoleStore<TUser>
和IUserPasswordStore<TUser>
两者都“继承”(实现)IUserStore
因此,如果我们需要角色管理功能,我们可以实现 IUserRoleStore<TUser>
:
class MyUser : IUser
{
// Additional properties and functions not shown for brevity.
}
class MyUserStore : IUserRoleStore<MyUser>
{
public bool IsInRole(string username, string role)
{
// Implementation not show for brevity.
}
/* We would then implement the rest of the required functions.
We would have a data context here that has access to users,
user-roles, and roles.
*/
}
现在我们可以通过 MyUserStore
至UserManager<TUser>
,因为MyUserStore
是 IUserRoleStore<TUser>
,这是一个IUserStore<TUser>
:
UserManager<MyUser> UM = new UserManager<MyUser>(new MyUserStore());
那么我怀疑 UserManager<TUser>
的源代码使用反射来查明在构造函数中传递给它的存储是否实现了 IUserStore<TUserStore>
之一的“子接口(interface)”,以便能够执行角色检查(如果它实现 IUserRoleStore<TUser>
)或密码设置/重置(如果它实现 IUserPasswordStore<TUser>
)。
我希望您发现这很有用,因为大多数文档(MVC 教程等)都没有告诉我们这个细节。他们告诉我们使用 UserStore<TUser>
Microsoft.AspNet.Identity.EntityFramework 的实现 - 我们所要做的就是传入自定义 User
对象(实现 IUser
),我们就可以开始了。
关于asp.net-identity - ASP.net 身份 - UserManager<TUser> 如何访问角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21321103/
尝试在 ASP.NET Core 2.0 中实现身份。我在解决这个问题时遇到了很多问题。 Startup.cs public class Startup { public Startup(IC
我已实现 Identity在 Asp.Net.Core MVC 3因为我们已经有了要使用的数据库表。调用下面的方法时出现错误,它们都发生在 UserManager 中在对方法的同一调用中。查看此方法的
我正在尝试使用以下内容自定义 asp.net 身份实体: public class User : IdentityUser public class UserClaim : IdentityUserC
我正在尝试自定义 ASP.NET Identity Core,但出现以下异常:(它似乎构建良好,但甚至在启动之前就崩溃了) Unhandled Exception: System.Reflection
当 Tuser 中的身份验证代码不为空时,这意味着用户没有在 24 小时内登录该站点,因此我必须删除这些行。 这是我写的查询,但没有正确删除 DELETE FROM tusers WHERE aut
我正在尝试为 asp.net Identity 2.0 实现自己的 DAL,并提供我需要的功能。我不需要帐户锁定功能。但是当我尝试打电话时 var result = await SignInManag
我有一个具有以下签名的 Controller : [Route("api/[controller]")] [ApiController] public class UsersController :
我正在尝试使用我需要的功能为 asp.net Identity 2.0 实现自己的 DAL。我不需要帐户锁定功能。但是当我尝试打电话时 var result = await SignInManager
我正在做一个数据库优先的项目。 我在 VS2013 中创建了一个新的 MVC5 站点。 我创建了一个具有电子邮件属性的 applicationUser: public class Applicatio
我正在为 IUserStore 编写自定义实现。创建方法的签名是: public async virtual Task CreateAsync(TUser user) 考虑到 Microsoft.As
在 visual studio 为 Web Api 2 项目的 ASP.NET 身份验证生成的模板项目中,我添加了一个类 ApplicationUserStore(这是我的自定义用户存储) publi
...我们只在 Microsoft.AspNet.Identity 内部。 (我们甚至没有查看 Microsoft.AspNet.Identity.EntityFramework 的基本实现。) Us
我有这个代码: public class BaanUserStore : IUserStore, IUserPasswordStore where TUser : Microsoft.AspNet.I
我正在使用 ASP.NET Core 2.1 身份。我已经覆盖了 IdentityUser,因为我需要为用户添加一些额外的属性。 在 Startup.cs 中 services.AddDefaultI
我注意到这个类有一个@符号。 我知道 $ 符号表示内部类。 那么,@ 是什么意思? 最佳答案 它只是类名和对象哈希码的十六进制表示形式之间的任意分隔符。正在 TUser 对象上调用 toString。
查看 ASP.NET Identity(ASP.NET 中的新成员身份实现),我在实现自己的 UserStore 时遇到了这个接口(interface)。 : //Microsoft.AspNet.I
我正在使用 Amazon.AspNetCore.Identity.Cognito 并且在使用 UserManager 的“ConfirmEmailAsync(TUser user, string to
我正在尝试使用 UserManager 更新用户但出现以下错误 The instance of entity type 'UserEntity' cannot be tracked because a
我是一名优秀的程序员,十分优秀!