Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

c# - Can i use more than role name in new ClaimTypes.Role

i have this methode add cookies with ClaimType Role

private async void AddCookies(string role)
    {
     var claim = new List<Claim>
     {
       new Claim(ClaimTypes.Role , role.ToString()),
     }
    }

and this my database table Users And Roels And Users Roles every user have more than role

i try to use array but not work with me like this example

 private async void AddCookies(string[] role)
        {
         var claim = new List<Claim>
         {
           new Claim(ClaimTypes.Role , role.ToString()),
         }
        }
question from:https://stackoverflow.com/questions/65915687/can-i-use-more-than-role-name-in-new-claimtypes-role

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are not adding a role for every element of the array, but you pass the whole array as parameter in Claim() constructor. You have to itterate through the array of roles and create for every single role new claim.

    private async void AddCookies(string[] roles)
    {
        var claims = new List<Claim>();

        foreach(var role in roles)
        {      
            var claim = new Claim(ClaimTypes.Role, role);

            claims.Add(claim);
        }       
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...