Mic*_*uso 4 user-management account-management asp.net-identity
...而我们只在Microsoft.AspNet.Identity中.(我们甚至没有查看Microsoft.AspNet.Identity.EntityFramework的基本实现.)
该UserManager班只需要在一个IUserStore<TUser>在构造函数中.它没有IUserRoleStore<TUserRole>我想象的需要访问以确定是否UserManager.IsInRoleAsync(string, string).
我认为UserStore的实现也会有一个IsInRoleAsync(string, string)功能(然后它会都有意义),但事实并非如此.
另一个奇怪的事情 - 如果UserManager在其实现中知道所有内容,那么UserManager如何才能执行密码设置和重置?我们正在处理IUser- 仅使用string Id和string UserName作为属性?
好吧,多挖掘和幸运发现后-事实证明,Microsoft.AspNet.Identity.Core带有一些其他的接口,特别是IUserRoleStore<TUser>和IUserPasswordStore<TUser> 这两个"继承"(实施)IUserStore <TUSER> .
因此,如果我们想要角色管理功能,我们实施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.
*/
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以传递MyUserStore给UserManager<TUser>,因为MyUserStore是一个IUserRoleStore<TUser>,这是一个IUserStore<TUser>:
UserManager<MyUser> UM = new UserManager<MyUser>(new MyUserStore());
Run Code Online (Sandbox Code Playgroud)
我怀疑,UserManager<TUser>使用反射的源代码来确定在构造函数中传入它的商店是否实现了一个IUserStore<TUserStore>"子接口",以便能够执行角色检查(如果它实现IUserRoleStore<TUser>)或密码集/ reset(如果它实现IUserPasswordStore<TUser>).
我希望你发现这很有用,因为大多数文档(MVC教程等)都没有告诉我们这个细节.他们告诉我们使用UserStore<TUser>Microsoft.AspNet.Identity.EntityFramework 的实现 - 我们所要做的就是传入一个自定义User对象(实现IUser),我们很高兴.
| 归档时间: |
|
| 查看次数: |
3509 次 |
| 最近记录: |