添加标识2.0角色自定义标识 [英] Adding Identity 2.0 Roles to custom Identity
问题描述
我最近在使用MVC 5和身份2.0新的应用程序开始工作时,才能使用我实现了本指南中详细介绍的自定义标识(的 HTTPS://$c$c.msdn.microsoft.com/ASPNET-45-MVC5-Custom-1a94ab26#内容)。
I recently started work on a new application using MVC 5 and Identity 2.0, in order to use a different password hashing algorithm I implemented the custom identity detailed in this guide (https://code.msdn.microsoft.com/ASPNET-45-MVC5-Custom-1a94ab26#content).
我已经看过结合角色各种方式进入这个身份实施,但到目前为止还没有发现让他们与这个新的身份执行工作的方式。
I have looked at various ways of incorporating roles into this identity implementation but so far have not found a way of making them work with this new identity implementation.
现在有没有人对如何去添加角色到一个类似的自定义身份提供指导的?
Does anyone now of a guide on how to go about adding roles to a similar custom identity provider?
任何指导将非常AP preciated。
Any guidance would be very much appreciated.
推荐答案
您实施 IdentityUser
( ApplicationUser
:如果你正在使用标准模板)将提供方法与角色的用户联系起来: AddToRoleAsync
, AddToRolesAsync
, GetRolesAsync
, RemoveFromRolesAsync
。
Your implementation of IdentityUser
(ApplicationUser
: if you're using the standard template) will provide the methods to associate a user with roles: AddToRoleAsync
, AddToRolesAsync
, GetRolesAsync
, RemoveFromRolesAsync
.
如果您要管理的角色,我怀疑,你必须添加一个 RoleManager< IdentityRole方式>
If you want to manage roles, as I suspect, you have to add a RoleManager<IdentityRole>
.
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
var appRoleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
return appRoleManager;
}
}
和它添加到owin方面:
and add this to the owin context:
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
在 ApplicationRoleManager 将允许您创建角色( CreateAsync
),发现( FindByIdAsync
),删除( DeleteAsync
)。
The ApplicationRoleManager will allow you to create roles (CreateAsync
), find (FindByIdAsync
), delete (DeleteAsync
).
虽然您的 ApplicationUserManager
public class ApplicationUserManager : UserManager<ApplicationUser>
{
...
}
将让你的角色与用户关联起来( AddToRoleAsync
),删除( RemoveFromRoleAsync
)。
如果您已经实现了您的 UserStore 使用接口 IUserStore
,那么你就需要实施 IUserRoleStore
以及
If you have implemented your UserStore using the interface IUserStore
, then you need to implement IUserRoleStore
as well.
在这最后的界面,你可以找到 AddToRoleAsync
, GetRolesAsync
, IsInRoleAsync
, RemoveFromRoleAsync
。
In this last interface you can find AddToRoleAsync
, GetRolesAsync
, IsInRoleAsync
, RemoveFromRoleAsync
.
您必须实现您的Rolestore的( IRoleStore
)以及
You have to implement your RoleStore (IRoleStore
) as well.
如果您想阅读有关这个话题的一些好文章,我建议你看看这个博客。
这家伙写了4篇文章至今关于 ASP.NET身份2.X
If you want to read some good articles about this topic I would suggest you to have a look at this blog. This guy has written 4 articles so far about ASP.NET Identity 2.x:
<一个href=\"http://bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/\"相对=nofollow>第1部分结果
<一href=\"http://bitoftech.net/2015/02/03/asp-net-identity-2-accounts-confirmation-password-user-policy-configuration/\"相对=nofollow>第2部分结果
<一href=\"http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/\"相对=nofollow>第3部分结果
<一href=\"http://bitoftech.net/2015/03/11/asp-net-identity-2-1-roles-based-authorization-authentication-asp-net-web-api/\"相对=nofollow>第4部分(你有兴趣一)
Part 1
Part 2
Part 3
Part 4 (the one you're interested in)
这是另一个家伙谁的话题写有趣的东西。
And this is another guy who writes interesting stuff on the topic.
这篇关于添加标识2.0角色自定义标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!