@SuppressWarnings("unchecked")
    protected final LogoutRequest buildLogoutRequest(final SAML2MessageContext context,
                                                     final Endpoint ssoService,
                                                     final SAML2Profile profile) {

        val builder = (SAMLObjectBuilder<LogoutRequest>) this.builderFactory
            .getBuilder(LogoutRequest.DEFAULT_ELEMENT_NAME);
        val request = builder.buildObject();

        val selfContext = context.getSAMLSelfEntityContext();

        request.setID(SAML2Utils.generateID());
        request.setIssuer(getIssuer(selfContext.getEntityId()));
        request.setIssueInstant(Instant.now().plusSeconds(this.issueInstantSkewSeconds));
        request.setVersion(SAMLVersion.VERSION_20);
        request.setDestination(ssoService.getLocation());

        // name id added (id of profile)
        val nameIdBuilder = (SAMLObjectBuilder<NameID>) this.builderFactory
            .getBuilder(NameID.DEFAULT_ELEMENT_NAME);
        val nameId = nameIdBuilder.buildObject();
        nameId.setValue(profile.getId());
        nameId.setFormat(profile.getSamlNameIdFormat());
        if (this.useNameQualifier) {
            nameId.setNameQualifier(profile.getSamlNameIdNameQualifier());
            nameId.setSPNameQualifier(profile.getSamlNameIdSpNameQualifier());
            nameId.setSPProvidedID(profile.getSamlNameIdSpProviderId());
        }
        request.setNameID(nameId);
        // session index added
        val sessIdx = profile.getSessionIndex();
        if (sessIdx != null) {
            val sessionIndexBuilder = (SAMLObjectBuilder<SessionIndex>) this.builderFactory
                .getBuilder(SessionIndex.DEFAULT_ELEMENT_NAME);
            val sessionIdx = sessionIndexBuilder.buildObject();
            sessionIdx.setValue(sessIdx);
            request.getSessionIndexes().add(sessionIdx);
        }
        return request;
    }
<p>buildLogoutRequest.</p>

@param context a {@link SAML2MessageContext} object
@param ssoService a {@link org.opensaml.saml.saml2.metadata.SingleLogoutService} object
@param profile a {@link SAML2Profile} object
@return a {@link LogoutRequest} object
pac4j-saml/src/main/java/org/pac4j/saml/logout/impl/SAML2LogoutRequestBuilder.java