function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sagar@sfdcsagar@sfdc 

how to check login credentials are correct or not

how to check the user provided credentials(password and username) are correct or incorrect .

 

this is my page /////////////////////////////

<apex:page controller="Beaconlogin" sidebar="false" showHeader="false" id="page1">
<apex:form id="form1">

<table >
          <tr>
            <td><apex:outputText value="User id" style="color:Black;font-size:16px;font-weight:bold"/></td>
            <td><apex:inputField value="{!login.User_id__c}" id="Userid2" style="color:Black;font-size:16px;font-weight:bold" /></td>
          </tr>
          <tr>
            <td><apex:outputText value="Password" style="color:Black;font-size:16px;font-weight:bold"/></td>
            <td><apex:inputsecret value="{!login.User_password__c}" id="Password2" style="color:Black;font-size:16px;font-weight:bold" /></td>
          </tr>
          <tr><td colspan="2">&nbsp;</td></tr>
          <tr><td colspan="2">&nbsp;</td></tr>
          <tr>
            <td></td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:commandButton value="Login" action="{!login_check}"  onclick="return logincheck()" id="Login"/></td>
          </tr>
        </table>

</apex:form>

</apex:page>

 

/////////////////////////////////controller ////////////////////////////////////

public with sharing class Beaconlogin {

 public PageReference login_check() {
   
      insert login;
       pagereference ref = new pagereference('https://c.ap1.visual.force.com/apex/ApplicationForm');
   ref.setredirect(true);
        return ref;
    }

 public Beacon_login__c login { get; set; }
    public Beaconlogin()
    {
    login= new Beacon_login__c();
    }

}

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

Bind the Username and Password variables with controller and on click call the action.In controller,In action query the table where you are keeping track of all username and password to find out whether username and password exist or not. If exist then allow the user to login else show a login fault message on the page.

 

 

All Answers

UVUV

Bind the Username and Password variables with controller and on click call the action.In controller,In action query the table where you are keeping track of all username and password to find out whether username and password exist or not. If exist then allow the user to login else show a login fault message on the page.

 

 

This was selected as the best answer
Tejpal KumawatTejpal Kumawat

Hi Sagar,

 

Use this Class:

 

public class TEZ_AC_VerifyPassword{
    public final String LOGIN_DOMAIN = 'login'; //other options: test, prerellogin.pre

    public String username {get{ return UserInfo.getUsername(); }}
    public transient String password {get;set;}

    public PageReference doVerify(){
        
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://' + LOGIN_DOMAIN + '.salesforce.com/services/Soap/u/22.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody(buildSoapLogin(username,password));

        //basically if there is a loginResponse element, then login succeeded; else there
        //  would be soap fault element after body
        final Boolean verified = (new Http()).send(request).getBodyDocument().getRootElement()
          .getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/')
          .getChildElement('loginResponse','urn:partner.soap.sforce.com') != null;

        if(verified) ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Correct password!'));
        else         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Incorrect password!'));

        return null;
    }

    public static String buildSoapLogin(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;
    }
}

 

 

And this page :

 

<apex:page controller="TEZ_AC_VerifyPassword">
    <apex:sectionHeader title="Verify Password"/>
    <apex:pageMessages />
    <apex:form >
        <apex:pageBlock title="" mode="view">
            <apex:pageBlockButtons >
                <apex:commandButton value="Verify" action="{!doVerify}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Username" for="username"/>
                    <apex:outputText value="{!username}" id="username"/> 
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Password" for="password"/>
                    <apex:inputSecret value="{!password}" id="password"/> 
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

--------------------
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Thanks

sagar@sfdcsagar@sfdc

hi tejpal,

 

   i think the solution u provided is for salesforce login , my requirement is for other site , if user exists he should redirect to another page(application form)

 

 

 

 

thanks

sagar

Tejpal KumawatTejpal Kumawat

Hi Sagar,

 

 

This is the same for other Sites also. Use there Http Request.

 

Thanks

sagar@sfdcsagar@sfdc

hi tejpal,

 

k thanks i vl try it with http request

 

 

sagar@sfdcsagar@sfdc

 hi,

     my requirement is if user exists and if it is first login page should redirect to resetpassword , if login count is more than one page should redirect to application form . if user does not exists error msg should be added ,

     after changing with query the record is not inserting ,

 

 

public Beacon_login__c loginc{ get; set; }

 public  PageReference login_check() {
    login =[select id,User_id__c,User_password__c from Beacon_login__c  ];
  integer count=0;
    count=[select count() from Beacon_login__c where id=:login.id ];
    if(count==1)
    {
        loginc=[select id,isReset__c from Beacon_login__c where id=:loginc.id ];
        if(loginc.isReset__c==false)
        {
               pagereference ref = new pagereference('https://c.ap1.visual.force.com/apex/Resetpassword');
               ref.setredirect(true);
        }
        else
        {
            pagereference ref = new pagereference('https://c.ap1.visual.force.com/apex/ApplicationForm');
               ref.setredirect(true);
        }
        
    }
    else{
    
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'User id or password are incorrect .'));
     
    }
    
   return null ;
       
    }

 public Beacon_login__c login { get; set; }
    public Beaconlogin()
    {
    login= new Beacon_login__c();
      } 

 

vl u plz correct and give me object name is  Beacon_login__c , fields are User_id__c,User_password__c


thanks 

sagar 

sagar@sfdcsagar@sfdc

hi

uv

 

after binding and query i got it , thanks for ur idea

 

 

 

thanks

sagar