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
clettebouclettebou 

SelfService User login

Hi
I'm trying to make my own SelfService User portal, and I encounter problems with login functionality.
Indeed, API don't offer capability to login SelfService User directly.
I see a lot of thread about delegated authentication, but I don't now how can I do that.
Can anyone send me Java or.NET sample code ?
Thanks

Clettebou
leadpointtechleadpointtech
I have a code example for you for redirecting a user to the login page.
Note that I have a POJO loginInfo that has four properties: orgId, username, password, url
These are set up before this code.
To get the orgId
Code:
    GetUserInfoResult userInfo = salesforceService.getUserInfo();
    loginInfo.setOrgId(userInfo.getOrganizationId());

For us on our test server, the loginInfo.url is https://tapp0.salesforce.com/sserv/login.jsp

Username and Password are the self service user's user name and password.  Your application needs to know these in order to log the user in.  You can get the username from salesforce using the contact info, but you will either have to pick a password, set it, and save it in a way available to your application or have the user enter their password.

Then you can redirect the user to the url returned in the following snippet (this code logs the user in then redirects them to the self service site home page) (NB this will only work if the ip address for your self service users is allowed to change - this is set up through customizing your account)

Code:
        String dataToPost = "orgId=" + loginInfo.getOrgId() + "&un=" + loginInfo.getUsername() + "&pw=" + loginInfo.getPassword();
        byte[] byteArray = dataToPost.getBytes("UTF-8");
        
        // log the user in
        HttpURLConnection connection = null;
        OutputStream requestStream = null;
        InputStream responseStream = null;
        BufferedReader rd = null;
        StringBuffer responseBuf = new StringBuffer();
        String redirectUrl = null;
        
        try {
            URL url = new URL(loginInfo.getUrl());
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);
            connection.connect();
            requestStream = connection.getOutputStream();
            requestStream.write(byteArray);
            requestStream.flush();
            responseStream = connection.getInputStream();
            rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            
            String line = "";
            while ((line = rd.readLine()) != null) {
                responseBuf.append(line);
            }
            
            // extract redirect url from response
            int beginIndexUrl = responseBuf.indexOf("<meta http-equiv=\"Refresh\" content=\"0; URL=") + 43;
            if (beginIndexUrl > 43) {
                int endIndexUrl = responseBuf.indexOf("\"", beginIndexUrl);
                redirectUrl = url.getProtocol() + "://" + url.getHost() + URLDecoder.decode(responseBuf.substring(beginIndexUrl, endIndexUrl), "UTF-8");
            }
            
        } finally {
            
            if (connection != null) {
                try {
                    connection.disconnect();
                } catch (Exception e) {
                    // do nothing
                }
            }
            
            if (requestStream != null) {
                try {
                    requestStream.close();
                } catch (Exception e) {
                    // do nothing
                }
            }
            
            if (responseStream != null) {
                try {
                    responseStream.close();
                } catch (Exception e) {
                    // do nothing
                }
            }
            
            if (rd != null) {
                try {
                    rd.close();
                } catch (Exception e) {
                    // do nothing
                }
            }
        }
        
        return redirectUrl;


clettebouclettebou
Thanks you!
It work.
But I have one another question.
How could I now if a SelfServiceUser password have been reset ?

Clettebou
sksonisksoni

Hi,  I tried using the code given below to login to SelfService portal, but was unable to connect.The response I received did not contain the '<meta' tag for refresh but the 'refresh' is added as a header. After the first successful request to login I am not able to reach the actual home page. The refresh url is exactly same as my initial url. Can somebody pls help me ?

sksonisksoni
Hi, finally i was able to login. I got the response as mentioned above with a little change.
I used http://urlToMySelfSrvicePortal instead of https.
I am a little concerned about using non https. Is this fine since i m logging into the portal
from my application ? Can anyone pls give me some suggestion on this ?
leadpointtechleadpointtech
My solution has stopped working because of a change to the page returned to the code after logging in.
This also points out a problem in that cookies being passed to the user are not actually getting set in the user's browser - it would be better if instead of sending the user to the redirect, the user is sent the entire response that salesforce sent to the code, headers and all.

I'm working on it, and will post here when I have new code.
SuperfellSuperfell
With Winter '07 there is an official API for handling login for self service users, see the LoginScope SOAP header.