Validate Salesforce User

 Here's a piece of code to verify the password of a user.

   


     

 HttpRequest request = new HttpRequest();

        request.setEndpoint('https://login.salesforce.com/services/Soap/u/22.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody(prepareSoapLoginRequest('me_raja1@test.com','QW@232323'));

        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, String.valueOf(new Http().send(request).getBody())));

 public static String prepareSoapLoginRequest(String username, String password){
        XmlStreamWriter w = new XmlStreamWriter();
        w.writeStartElement('', 'login', 'urn:partner.soap.sforce.com');
        w.writeNamespace('', 'urn:partner.soap.sforce.com');
        w.writeStartElement('', 'username', 'urn:partner.soap.sforce.com');
        w.writeCharacters(username);
        w.writeEndElement();
        w.writeStartElement('', 'password', 'urn:partner.soap.sforce.com');
        w.writeCharacters(password);
        w.writeEndElement();
        w.writeEndElement();  
        
        String xmlOutput = 
              '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body>' 
            + w.getXmlString() 
            + '</Body></Envelope>';
        w.close();
        return xmlOutput;

    }


For Correct Password

{
"Envelope": {
"Body": {
"loginResponse": { 
"result": {
"metadataServerUrl": "https://sfidentity2-dev-ed.my.salesforce.com/services/Soap/m/22.0/00D80000000aNi9",
"passwordExpired": "false",
"sandbox": "false",
"serverUrl": "https://sfidentity2-dev-ed.my.salesforce.com/services/Soap/u/22.0/00D80000000aNi9",
"sessionId": "00D80000000aNi9!ARAAQGgAOwj9Xe.zSZp5GbWTXxCcNQXAzgdmXO.l7fPcdrVJS9.Ma4mjazDbXurXUGyh5cM6iyl_iX2eV1qCY9BlcNSY0ZY4",
"userId": "00580000001oMvfAAE",
"userInfo": {
"accessibilityMode": "false",
"chatterExternal": "false",
"currencySymbol": "$",
"orgAttachmentFileSizeLimit": "5242880",
"orgDefaultCurrencyIsoCode": "USD",
"orgDefaultCurrencyLocale": "en_US",
"orgDisallowHtmlAttachments": "false",
"orgHasPersonAccounts": "false",
"organizationId": "00D80000000aNi9EAE",
"organizationMultiCurrency": "false",
"organizationName": "Planman Tech",
"profileId": "00e800000012hbtAAA",
"roleId": "00E80000000zRnEEAU",
"sessionSecondsValid": "7200",
"userDefaultCurrencyIsoCode": {
"_xsi:nil": "true"
},
"userEmail": "rpatnaik@test.com",
"userFullName": "Raja Patnaik",
"userId": "005800000015565AQS",
"userLanguage": "en_US",
"userLocale": "en_US",
"userName": "me_raja1@test.com",
"userTimeZone": "America/New_York",
"userType": "Standard",
"userUiSkin": "Theme3"
}
}
},
"__prefix": "soapenv"
},
"_xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"_xmlns": "urn:partner.soap.sforce.com",
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"__prefix": "soapenv"
}

}

For Wrong Password

{
"Envelope": {
"Body": {
"Fault": {
"faultcode": "sf:INVALID_LOGIN",
"faultstring": "INVALID_LOGIN: Invalid username, password, security token; or user locked out.",
"detail": {
"LoginFault": {
"exceptionCode": {
"__prefix": "sf",
"__text": "INVALID_LOGIN"
},
"exceptionMessage": {
"__prefix": "sf",
"__text": "Invalid username, password, security token; or user locked out."
},
"_xsi:type": "sf:LoginFault",
"__prefix": "sf"
}
},
"__prefix": "soapenv"
},
"__prefix": "soapenv"
},
"_xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"_xmlns:sf": "urn:fault.partner.soap.sforce.com",
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"__prefix": "soapenv"
}

}


Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach