Hello.

 

I am using Keycloak admin client version 1.7.0. Final to create users from an external application. I am able to create Users (with user.setEnabled set to true)and set the password as well. However, when I try to login it fails.

 

Please can anyone show me a sample code for password reset?

 

This is what I have so far:

 

URL: http://localhost:8888/auth/admin/realms/crusadersterling/users/

 

public void resetPassword(String userId, String token) throws IOException, Failure {

 

                              CredentialRepresentation credential = new CredentialRepresentation();

                              credential.setType(CredentialRepresentation.PASSWORD);

                              credential.setValue("123456");

                              credential.setTemporary(true);

 

                              HttpClient client = HttpClientBuilder.create().build();

                              ObjectMapper mapper = new ObjectMapper();

                              String json = null;

                              try {

                                             String usesUrl = PropertiesUtil.getValue("reset_password");

                                             HttpPut put = new HttpPut(usesUrl + userId + "/reset-password");

                                             put.addHeader("Authorization", "Bearer " + token);

                                             put.setHeader("Content-Type", "application/json");

                                             put.setHeader("Accept", "application/json");

                                             json = mapper.writeValueAsString(credential);

                                             StringEntity entity = new StringEntity(json);

                                             put.setEntity(entity);

                                             try {

                                                            HttpResponse response = client.execute(put);

 

                                                           

 

                                                            if (!String.valueOf(response.getStatusLine().getStatusCode()).startsWith("20")) {

                                                                           System.err.println(response.getStatusLine().getReasonPhrase());

                                                                           throw new Failure(response.getStatusLine().getStatusCode());

                                                            }

 

                                             } catch (IOException | Failure | IllegalStateException e) {

 

                                                            throw e;

                                             }

                              } catch (IOException e) {

                                             throw new RuntimeException(e);

                              } finally {

                                             client.getConnectionManager().shutdown();

                              }

                             

               }

 

Sent from Mail for Windows 10