Owin索赔-添加多个ClaimTypes.Role [英] Owin claims - Add multiple ClaimTypes.Role

查看:81
本文介绍了Owin索赔-添加多个ClaimTypes.Role的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以为用户分配以下角色的应用程序:

I have an application in which users can be assigned the following roles:

  • SuperAdmin
  • 管理员
  • 用户

一个用户可能已分配了两个或多个角色,例如.SuperAdmin和User.我的应用程序使用声明,因此我也想通过声明对用户角色进行身份验证.像:

One user may have assigned two or more roles, eg. both SuperAdmin and User. My application uses claims, and therefore i want to authenticate user roles through claims too. like:

[Authorize(Roles="Admin")]

不幸的是,我不知道如何为我的ClaimTypes.Role添加多个角色.我有以下代码:

Unfortunately, i dont know how i can add multiple roles to my ClaimTypes.Role. I have the following code:

var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Name, name),
                new Claim(ClaimTypes.Email, email),
                new Claim(ClaimTypes.Role, "User", "Admin", "SuperAdmin")
        },
            "ApplicationCookie");

如您所见,为了说明起见,我尝试添加更多角色,但是显然这样做是错误的,因此不起作用.因此,非常感谢您的帮助.

As you can see, i tried to add more roles for the sake of illustrating, but obviously its done in a wrong way, and therefore doesn't work. Any help is therefore much appreciated.

推荐答案

一个声明标识可以有多个具有相同ClaimType的声明.这样便可以使用 HasClaim 方法来检查是否存在特定的用户角色.

A claims identity can have multiple claims with the same ClaimType. That will make it possible to use the HasClaim method for checking if a specific user role is present.

var identity = new ClaimsIdentity(new[] {
            new Claim(ClaimTypes.Name, name),
            new Claim(ClaimTypes.Email, email),
            new Claim(ClaimTypes.Role, "User"),
            new Claim(ClaimTypes.Role, "Admin"), 
            new Claim(ClaimTypes.Role,"SuperAdmin")
    },
        "ApplicationCookie");

这篇关于Owin索赔-添加多个ClaimTypes.Role的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆