ASP.NET标识-将角色用作预定义的声明集

ASP.NET标识-将角色用作预定义的声明集,asp.net,asp.net-mvc,asp.net-identity,claims-based-identity,Asp.net,Asp.net Mvc,Asp.net Identity,Claims Based Identity,我不熟悉ASP.NET Identity,但从我所读到的内容来看,我的任务应该可以通过它来实现 我正在考虑将角色和声明结合起来 角色用于抽象描述用户在公司中的职能(“管理”、“销售”、“支持”等) 声明用于具体说明其所有者可以做什么(“创建用户”、“编辑支持请求”、“重置密码”) 多个声明分配给角色,表示反映每个角色权限的“权限集” 资源受可能需要角色或声明的策略保护 这通常是使用ASP.NET标识的可行方法吗 我在这里找到了一个很有希望的方法:这看起来很像是我需要的基础,博客作者正在使用r

我不熟悉ASP.NET Identity,但从我所读到的内容来看,我的任务应该可以通过它来实现

我正在考虑将角色声明结合起来

  • 角色用于抽象描述用户在公司中的职能(“管理”、“销售”、“支持”等)
  • 声明用于具体说明其所有者可以做什么(“创建用户”、“编辑支持请求”、“重置密码”)
  • 多个声明分配给角色,表示反映每个角色权限的“权限集”
  • 资源受可能需要角色或声明的策略保护
这通常是使用ASP.NET标识的可行方法吗

我在这里找到了一个很有希望的方法:这看起来很像是我需要的基础,博客作者正在使用
rolemager.addclaimersync()
向角色添加声明。但是,该功能似乎不是2015年2月推出的最新
Microsoft.AspNet.Identity.Core
Nuget软件包v2.2.1的一部分,而是在这里实现的: 我是不是把一些东西混在一起了?

当你看的时候,角色是一种权利类型。基本上,角色是索赔的一个子集

这完全取决于你计划如何使用它们。如果使用默认的Authorize过滤器,则需要将它们添加为ClaimTypes.Role。查看
claims.Add(新的claims(ClaimTypes.Role,roleName))在下面的示例中

public class CustomAuthenticationService
{
    private readonly HttpContextBase _context;
    private const string AuthenticationType = "ApplicationCookie";

    public CustomAuthenticationService(HttpContextBase context)
    {
        _context = context;
    }

    public void SignIn(User user, IList<string> roleNames)
    {
        IList<Claim> claims = new List<Claim>
        {
            new Claim(ClaimTypes.Sid, user.Id.ToString()),
            new Claim(ClaimTypes.Name, user.UserName),
            new Claim(ClaimTypes.GivenName, user.FirstName),
            new Claim(ClaimTypes.Surname, user.LastName),
        };

        foreach (string roleName in roleNames)
        {
            claims.Add(new Claim(ClaimTypes.Role, roleName));
        }

        ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignIn(identity);
    }

    public void SignOut()
    {
        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignOut(AuthenticationType);
    }
}
公共类CustomAuthenticationService
{
私有只读HttpContextBase\u上下文;
private const string AuthenticationType=“applicationcokie”;
公共CustomAuthenticationService(HttpContextBase上下文)
{
_上下文=上下文;
}
公共无效登录(用户,IList roleNames)
{
IList索赔=新清单
{
新声明(ClaimTypes.Sid,user.Id.ToString()),
新索赔(ClaimTypes.Name、user.UserName),
新索赔(ClaimTypes.GivenName,user.FirstName),
新索赔(ClaimTypes.LastName、user.LastName),
};
foreach(roleNames中的字符串roleName)
{
claims.Add(新索赔(ClaimTypes.Role,roleName));
}
索赔实体标识=新的索赔实体(索赔、认证类型);
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignIn(标识);
}
公共无效签出()
{
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignOut(AuthenticationType);
}
}
意见 我个人不喜欢创建自定义声明名称,例如“创建用户”、“编辑支持请求”、“重置密码”


相反,我会将它们保留为角色,例如
用户创建
用户编辑
用户删除
,这样我就可以使用Authorize属性。

从技术上讲,角色应该是权限:“创建用户”等。大多数开发人员不正确地使用角色,而Microsoft将角色与“组”合并也无济于事,例如在Windows Auth的情况下。声明应该是与“用户”关联的瞬态数据。当没有实际的“用户”对象(即数据库中的记录)时,例如在执行OAuth时,它们最适用。如果您有用户记录,通常最好将数据随附在一起。您引用的github链接适用于.Net Core、Identity v3和下一代。版本2.2.1在这里:aspnetidentity.codeplex.com
ClaimTypes.Role
听起来使用起来很合理,谢谢。尽管如此,我在遇到的所有示例中都遗漏了一些,我不确定的是具体权限的组合,如
用户创建
用户编辑
和抽象,命名权限“集合”或“模板”,如
编辑器
管理员
,它们本质上是以前具体权限的集合。这对我来说似乎是一个非常普遍的要求,所以我原本希望Identity framework能够开箱即用。话虽如此,我可能对显而易见的事情视而不见。