예제 #1
0
        public async Task <Tuple <bool, string[]> > UpdateRoleAsync(IApplicationRole role, IEnumerable <string> claims)
        {
            if (claims != null)
            {
                string[] invalidClaims = claims.Where(claim => AuthorizationManager.GetByValue(claim) == null).ToArray();
                if (invalidClaims.Any())
                {
                    return(Tuple.Create(false, new[] { "The following claim types are invalid: " + string.Join(", ", invalidClaims) }));
                }
            }

            var result = await roleManager.UpdateAsync(role as ApplicationRole);

            if (!result.Succeeded)
            {
                return(Tuple.Create(false, result.Errors.Select(e => e.Description).ToArray()));
            }


            if (claims != null)
            {
                var roleClaims      = (await roleManager.GetClaimsAsync(role as ApplicationRole)).Where(c => c.Type == ApplicationClaimType.Authorization);
                var roleClaimValues = roleClaims.Select(c => c.Value).ToArray();

                var claimsToRemove = roleClaimValues.Except(claims).ToArray();
                var claimsToAdd    = claims.Except(roleClaimValues).Distinct().ToArray();

                if (claimsToRemove.Any())
                {
                    foreach (string claim in claimsToRemove)
                    {
                        result = await roleManager.RemoveClaimAsync(role as ApplicationRole, roleClaims.Where(c => c.Value == claim).FirstOrDefault());

                        if (!result.Succeeded)
                        {
                            return(Tuple.Create(false, result.Errors.Select(e => e.Description).ToArray()));
                        }
                    }
                }

                if (claimsToAdd.Any())
                {
                    foreach (string claim in claimsToAdd)
                    {
                        result = await roleManager.AddClaimAsync(role as ApplicationRole, new Claim(ApplicationClaimType.Authorization, AuthorizationManager.GetByValue(claim)));

                        if (!result.Succeeded)
                        {
                            return(Tuple.Create(false, result.Errors.Select(e => e.Description).ToArray()));
                        }
                    }
                }
            }

            return(Tuple.Create(true, new string[] { }));
        }
예제 #2
0
        public async Task <Tuple <bool, string[]> > CreateRoleAsync(IApplicationRole role, IEnumerable <string> claims)
        {
            if (claims == null)
            {
                claims = new string[] { };
            }

            string[] invalidClaims = claims.Where(claim => AuthorizationManager.GetByValue(claim) == null).ToArray();
            if (invalidClaims.Any())
            {
                return(Tuple.Create(false, new[] { "The following claim types are invalid: " + string.Join(", ", invalidClaims) }));
            }


            var result = await roleManager.CreateAsync(role as ApplicationRole);

            if (!result.Succeeded)
            {
                return(Tuple.Create(false, result.Errors.Select(error => error.Description).ToArray()));
            }


            role = await roleManager.FindByNameAsync(role.Name);

            foreach (string claim in claims.Distinct())
            {
                result = await roleManager.AddClaimAsync(role as ApplicationRole, new Claim(ApplicationClaimType.Authorization, AuthorizationManager.GetByValue(claim)));

                if (!result.Succeeded)
                {
                    await DeleteRoleAsync(role);

                    return(Tuple.Create(false, result.Errors.Select(e => e.Description).ToArray()));
                }
            }

            return(Tuple.Create(true, new string[] { }));
        }