Пример #1
0
        private IEnumerable <Claim> GetClaimsFromUser(User user)
        {
            List <Claim> claims = null;

            if (user != null)
            {
                claims = new List <Claim>
                {
                    new Claim(JwtClaimTypes.Subject, user.Id.ToString()),
                    new Claim(JwtClaimTypes.PreferredUserName, user.UserName),
                    new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName)
                };

                var userId = user.Id.ToString();
                claims.Add(new Claim(ClaimsConstants.UserIdClaimType, userId));

                var tenantId = user.HasTenant ? user.TenantId.Value.ToString() : ClaimsConstants.NullValue;
                claims.Add(new Claim(ClaimsConstants.TenantIdClaimType, tenantId));

                var name = !string.IsNullOrWhiteSpace(user.Name) ? user.Name : ClaimsConstants.NullValue;
                claims.Add(new Claim(ClaimsConstants.NameClaimType, name));

                var lastName = !string.IsNullOrWhiteSpace(user.LastName) ? user.LastName : ClaimsConstants.NullValue;
                claims.Add(new Claim(ClaimsConstants.NameClaimType, lastName));

                var userPictureUrl = user.HasPicture
                    ? UrlExtensions.Combine(_settings.ExternalEndpoint, PictureEndpoint)
                    : ClaimsConstants.NullValue;
                claims.Add(new Claim(JwtClaimTypes.Picture, userPictureUrl));

                if (_userManager.SupportsUserEmail)
                {
                    string email          = !string.IsNullOrWhiteSpace(user.Email) ? user.Email : ClaimsConstants.NullValue;
                    string emailConfirmed = user.EmailConfirmed ? ClaimsConstants.TrueValue : ClaimsConstants.FalseValue;

                    claims.AddRange(new[]
                    {
                        new Claim(JwtClaimTypes.Email, email),
                        new Claim(JwtClaimTypes.EmailVerified, emailConfirmed, ClaimValueTypes.Boolean)
                    });
                }

                if (_userManager.SupportsUserPhoneNumber)
                {
                    string phoneNumber          = !string.IsNullOrWhiteSpace(user.PhoneNumber) ? user.PhoneNumber : ClaimsConstants.NullValue;
                    string phoneNumberConfirmed = user.PhoneNumberConfirmed ? ClaimsConstants.TrueValue : ClaimsConstants.FalseValue;

                    claims.AddRange(new[]
                    {
                        new Claim(JwtClaimTypes.PhoneNumber, phoneNumber),
                        new Claim(JwtClaimTypes.PhoneNumberVerified, phoneNumberConfirmed, ClaimValueTypes.Boolean)
                    });
                }

                var roles = !string.IsNullOrWhiteSpace(user.Roles) ? user.Roles : ClaimsConstants.NullValue;
                claims.Add(new Claim(ClaimsConstants.RolesClaimType, roles));

                var permissions = !string.IsNullOrWhiteSpace(user.Permissions) ? user.Permissions : ClaimsConstants.NullValue;
                claims.Add(new Claim(ClaimsConstants.PermissionsClaimType, permissions));

                var languageCode = !string.IsNullOrWhiteSpace(user.LanguageCode) ? user.LanguageCode : ClaimsConstants.NullValue;
                claims.Add(new Claim(ClaimsConstants.LanguageCodeClaimType, languageCode));
            }

            return(claims);
        }