Exemple #1
0
        public static IIdentity ToIdentity(this OidcUser user)
        {
            var identity = new ClaimsIdentity();

            if (user == null || user.Data.IsEmpty())
            {
                return(identity);
            }

            var claims = new List <Claim>
            {
                CreateClaim(user, user.NameClaimKey, user.Value <string>(user.NameClaimKey), true)
            };

            var roles = user.Data.ValueAsArray <string>(user.RolesClaimKey);

            if (roles.IsNotEmpty())
            {
                foreach (var role in roles)
                {
                    claims.AddIfNotEmpty(CreateClaim(user, user.RolesClaimKey, role, false));
                }
            }

            return(new ClaimsIdentity(claims, "OpenIdConnect", user.NameClaimKey, user.RolesClaimKey));
        }
Exemple #2
0
        private static Claim CreateClaim(OidcUser user, string name, string value, bool required)
        {
            if (value.IsEmpty())
            {
                if (!required)
                {
                    return(null);
                }
                throw new OidcException($"OidcUser profile does not contain the claim \"{name}\"");
            }

            return(new Claim(name, value, ClaimValueTypes.String, user.Issuer));
        }
Exemple #3
0
        public static AuthenticationState ToAuthenticationState(this OidcUser user)
        {
            var principal = new ClaimsPrincipal(user.ToIdentity());

            return(new AuthenticationState(principal));
        }