问题描述
...,而我们仅位于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
作为属性,那么UserManager如何能够执行密码设置和重置?/p>
好吧,经过大量挖掘和幸运的发现-事实证明,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.
*/
}
现在我们可以将MyUserStore
传递给UserManager<TUser>
,因为MyUserStore
是IUserRoleStore<TUser>
,这是IUserStore<TUser>
:
UserManager<MyUser> UM = new UserManager<MyUser>(new MyUserStore());
然后,我怀疑UserManager<TUser>
的源代码使用反射来查找构造函数中传递给它的存储是否实现了IUserStore<TUserStore>
的子接口"之一,以便能够执行角色
IUserRoleStore<TUser>
)或设置/重置密码(如果它实现了IUserPasswordStore<TUser>
).我希望您觉得这很有用,因为大多数文档(MVC教程等)都不会告诉我们这一细节.他们告诉我们使用Microsoft.AspNet.Identity.EntityFramework的UserStore<TUser>
实现-我们要做的就是传递自定义的User
对象(实现IUser
),我们很高兴./p>
... and we're only inside Microsoft.AspNet.Identity. (We're not even looking at the base implementation at Microsoft.AspNet.Identity.EntityFramework.)
The UserManager
class only takes in an IUserStore<TUser>
at constructor. It does not have an IUserRoleStore<TUserRole>
which I imagine would need to be accessed to determine whether UserManager.IsInRoleAsync(string, string)
or not.
I'm thinking UserStore's implementation would have an IsInRoleAsync(string, string)
function as well (then it would all make sense), but it does not.
Another strange thing - how can UserManager be able to perform password setting and resetting if all it knows inside its implementation is that we're dealing with an IUser
- with only string Id
and string UserName
as properties?
Alright, after much digging and lucky discovery - it turns out that Microsoft.AspNet.Identity.Core comes with a few other interfaces, in particular, IUserRoleStore<TUser>
and IUserPasswordStore<TUser>
which both "inherit"(implement) IUserStore<TUser>.
So if we wanted role management capabilities, we implement 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.
*/
}
Now we can pass MyUserStore
to UserManager<TUser>
, because MyUserStore
is an IUserRoleStore<TUser>
, which is an IUserStore<TUser>
:
UserManager<MyUser> UM = new UserManager<MyUser>(new MyUserStore());
I suspect, then, that the source code for UserManager<TUser>
uses reflection to find out whether the store passed into it at constructor implements one of IUserStore<TUserStore>
's "child interfaces", in order to be able to perform role checks (if it implements IUserRoleStore<TUser>
) or password set/reset (if it implements IUserPasswordStore<TUser>
).
I hope you find this useful because most documentation (MVC tutorials and such) don't tell us this detail. They tell us to use the UserStore<TUser>
implementation of Microsoft.AspNet.Identity.EntityFramework - where all we have to do is pass in a custom User
object (that implements IUser
) and we're good to go.
这篇关于ASP.net身份-UserManager< TUser>有权使用角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!