【发布时间】:2013-10-23 12:01:19
【问题描述】:
如何在新的 ASP.NET 身份系统 (1.0) 中添加角色?
有一个UserStore 类,但没有RoleStore 类。
我找不到有关此问题的任何文档。
【问题讨论】:
-
有一个简单的角色提供者可以让你通过角色限制对应用程序部分的访问。您可以轻松创建“管理员”等角色并将用户添加到角色。
如何在新的 ASP.NET 身份系统 (1.0) 中添加角色?
有一个UserStore 类,但没有RoleStore 类。
我找不到有关此问题的任何文档。
【问题讨论】:
RoleManager = new RoleManager<IdentityRole>(
new RoleStore<IdentityRole>(new MyDbContext()));
var roleresult = RoleManager.Create(new IdentityRole(roleName));
【讨论】:
RoleManagerFactory = () => new RoleManager<IdentityRole>(new RoleStore<IdentityRole>()); 然后复制UserManagerFactory的用法。
从 .NET Framework 4.5 开始,Windows Identity Foundation (WIF) 已完全集成到 .NET Framework 中。
我建议检查通过声明实施授权 (Expressing Roles as Claims) 的可能性,在我看来是首选。
调用 IsInRole() 方法时,会检查当前用户是否具有该角色。 在声明感知应用程序中,角色由令牌中应该可用的角色声明类型表示。
角色声明类型使用以下 URI 表示: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
所以从 UserManager 你可以做这样的事情(没有 RoleManager):
var um = new UserManager();
um.AddClaimAsync(1, new Claim(ClaimTypes.Role, "administrator"));
声明可以简化和提高身份验证和授权过程的性能。 您可以使用存储为声明的角色来消除每次授权时的后端查询。
使用声明,您将不再需要 RoleStore(至少出于等效授权目的...)
【讨论】:
我在一个示例 asp.net 网页 page_load 中使用了下面的 sn-ps 来开始掌握 ASP Identity 的工作方式
UserManager userManager = new UserManager();
var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
var roleManager = new RoleManager<IdentityRole>(roleStore);
var applicationRoleAdministrator = new IdentityRole("superadmin");
if (!roleManager.RoleExists(applicationRoleAdministrator.Name))
{
roleManager.Create(applicationRoleAdministrator);
}
ApplicationUser applicationUserAdministrator = userManager.FindByName(User.Identity.Name);
if (!userManager.GetRoles(applicationUserAdministrator.Id).Contains("superadmin"))
{
Response.Redirect("~/account/login.aspx?ReturnUrl=" + Request.Url.AbsolutePath);
}
当然,ApplicationDbContext 下面是使用 ASP.NET 4.5+ 模板自动生成的,如下所示
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
也创建应用程序角色管理器类
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
//return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
return new ApplicationRoleManager(new RoleStore<IdentityRole>(new ApplicationDbContext()));
}
}
还在您的 startup.Auth.cs => ConfigureAuth(IAppBuilder app) 方法中添加以下行
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
然后在你的控制器中:
private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager
{
get
{
return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
}
private set
{
_roleManager = value;
}
}
我是这个 Identity Stuff 的新手,我不确定是否有必要,或者我做得干净正确,但这些步骤对我有用
【讨论】:
ASP.NET 身份是关于角色的声明。这真的让我很困惑,因为在以前的系统中,您在 web.config 中配置了成员资格和角色提供者。
对我来说问题是我有这样的代码:
HttpContext.Current.User.IsInRole("some role")
幸运的是,这个逻辑仍然有效。您可以在 Microsoft.AspNet.Identity.Core 中的 ClaimsIdentityFactory.cs 中看到 CreateAsync 函数中的逻辑。其中一个论点是UserManager。它询问它是否为SupportsUserRole,如果是,则调用GetRolesAsync,并将每个角色作为声明添加到ClaimIdentity。没有必要自己做。
IsInRole 使用此处描述的声明:
【讨论】:
RoleManager;它位于 Microsoft.AspNet.Identity 命名空间中,您会发现它被用于我相信的几个示例/入门应用程序中。