Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Reset SAP User Password

Former Member
0 Likes
7,956

Hi,

I need to do a RFC to other system reset SAP passwords. I found BAPI_USER_CHANGE, and return user was updated but it doesn't work when I try to log using this new password.

Could someone help me?


DATA:

  t_return    TYPE STANDARD TABLE OF bapiret2,

  w_password  TYPE bapipwd                   ,

  w_passwordx TYPE bapipwdx                  ,

  w_return    TYPE bapiret2                  ,

  v_user      TYPE bapibname-bapibname       .

v_user = sy-uname.

w_password-bapipwd = 'NewPWD@123'.

w_passwordx-bapipwd = 'X'         .

CALL FUNCTION 'BAPI_USER_CHANGE'

  EXPORTING

    username = v_user

    password = w_password

    passwordx = w_passwordx

  TABLES

    return   = t_return.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

LOOP AT t_return INTO w_return.

  WRITE: / w_return-message.

ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
6,243

Our basis is gonna disable CUA, and reset is gonna be made by web app using a RFC that I made.

That's the code:

obs: I'm gonna make some corrections such return after error in BAPI_USER_CHANGE


FUNCTION y_change_user_password.

*"----------------------------------------------------------------------

*"*"Interface local:

*"  IMPORTING

*"     VALUE(USUARIO) TYPE  BAPIBNAME-BAPIBNAME

*"     VALUE(SENHA) TYPE  BAPIPWD-BAPIPWD

*"     VALUE(DESBLOQUEIA) TYPE  CHAR1 OPTIONAL

*"  EXPORTING

*"     VALUE(T_RETORNO) TYPE  BAPIRET2_TAB

*"----------------------------------------------------------------------

  TYPE-POOLS: abap.

  DATA:

     w_pwd_reset   TYPE bapipwd    ,

     w_pwd_x       TYPE bapipwdx   ,

     w_retorno     TYPE bapiret2   ,

     v_usr_existe  TYPE i          ,

     v_usuario     TYPE string     ,

     v_senha_reset TYPE rsyst-bcode,

     v_usr_sap     TYPE sy-uname   ,

     v_nova_senha  TYPE rsyst-bcode,

     w_return      TYPE bapiret2   .

  v_usuario          = usuario          .

  w_pwd_reset-bapipwd = 'Mudar@123$reset'.                  "#EC NOTEXT

* Remove espaço no nome do usuário e senha

  CONDENSE v_usuario NO-GAPS.

* Dados de logon (utilização parte do núcleo)

  SELECT COUNT( DISTINCT bname )

   FROM usr02

   INTO v_usr_existe

   WHERE bname = usuario.

* Retorna erro se o usuário não existir

  IF v_usr_existe = 0.

    w_retorno-type   = 'W'                           .     "#EC NOTEXT

    w_retorno-message = 'Usuário & não existe no SAP' .     "#EC NOTEXT

    REPLACE '&' WITH v_usuario INTO w_retorno-message .

    APPEND w_retorno TO t_retorno                     .

  ELSE.

*   Verifica se é para desbloquear o usuário

    IF desbloqueia IS NOT INITIAL.

*     Desbloqueia o usuário

      CALL FUNCTION 'BAPI_USER_UNLOCK'

        EXPORTING

          username = usuario

        TABLES

          return  = t_retorno.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

    ENDIF.

*   Retorna erro se a senha não tiver sido informada

    IF senha IS INITIAL.

      w_retorno-type   = 'E'                                        . "#EC NOTEXT

      w_retorno-message = 'Senha não foi informada para o usuário & '. "#EC NOTEXT

      REPLACE '&' WITH v_usuario INTO w_retorno-message              .

      APPEND w_retorno TO t_retorno                                  .

    ELSE.

*     Reseta a senha do usuário

      w_pwd_x-bapipwd = abap_true.

      CALL FUNCTION 'BAPI_USER_CHANGE'

        EXPORTING

          username = usuario

          password = w_pwd_reset

          passwordx = w_pwd_x

        TABLES

          return   = t_retorno.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

      v_usr_sap    = usuario            .

      v_senha_reset = w_pwd_reset-bapipwd.

      v_nova_senha = senha              .

*     Altera a senha do usuário com a nova informada na RFC

      CALL FUNCTION 'SUSR_USER_CHANGE_PASSWORD_RFC'

        EXPORTING

          bname                    = v_usr_sap

          password                 = v_senha_reset

          new_password             = v_nova_senha

        IMPORTING

          return                   = w_return

        EXCEPTIONS

          change_not_allowed       = 1

          password_not_allowed     = 2

          internal_error           = 3

          canceled_by_user         = 4

          password_attempts_limited = 5

          OTHERS                   = 6.

      IF sy-subrc <> 0.

        w_retorno-type   = 'E'                          .  "#EC NOTEXT

        w_retorno-message = w_return-message             .  "#EC NOTEXT

        REPLACE '&' WITH v_usuario INTO w_retorno-message.

        APPEND w_retorno TO t_retorno                    .

      ENDIF.

    ENDIF.

  ENDIF.

ENDFUNCTION.

18 REPLIES 18
Read only

Former Member
0 Likes
6,243

What is in the return table after the BAPI?

Are you using upper and lower case when logging in?

Now that I look at it more closely, I'm not sure you can change your own password this way.

Rob

Message was edited by: Rob Burbank

Read only

0 Likes
6,243

It's returning "User & has changed":

When I try to login again, I need to use my old password. The new one doesn't work

I saw something about use a RFC destination with SAP all user, but I didn't understand well...

Read only

0 Likes
6,243

Try running it under a different userid.

Rob

Read only

0 Likes
6,243

I've tested with other user and... same problem, the return is ok but doesn't work

Read only

0 Likes
6,243

What I meant was; hardcode your userid in the program and run it from a different one.

Rob

Read only

0 Likes
6,243

I tested and didn't work either... Strange, isn't it?

Read only

0 Likes
6,243

A shot in the dark: instead of

w_password-bapipwd = 'XXXXX'.

w_passwordx-bapipwd = 'X'.

Try:

w_password ='XXXX'.

w_password = 'X'.

(I've seen it used this way where it works.)

Rob

Read only

Former Member
0 Likes
6,243

Hi,

Check passing the destination system as below.


CALL FUNCTION 'BAPI_USER_CHANGE' <DESTINATION SYSTEM>

  EXPORTING

     username = v_user

     password = w_password

     passwordx = w_passwordx

   TABLES

     return   = t_return.

Hope this helps you.

Thanks

KH

Read only

0 Likes
6,243

Thank you, , but your tip didn't work

do you know how can I create this destination? Do I need a sap all user in this rfc destination...?

Read only

0 Likes
6,243

Hi,

Do you have multiple systems in which user password needs to be changed or else only one system ?.

Can you let me know the logical system(LOGSYS) name in which you need to change the user password.

Thanks

KH

Read only

0 Likes
6,243

Hi,

Do you have multiple systems in which user password needs to be changed or else only one system ?.

Can you let me know the logical system(LOGSYS) name in which you need to change the user password.

Thanks

KH

Read only

0 Likes
6,243

Hi,

I made this other test and still not working...

Do you have any idea...? I heard we use "CUA" here, but I don't know this problem is caused by it


DATA:

  t_return    TYPE STANDARD TABLE OF bapiret2,

  w_password  TYPE bapipwd                   ,

  w_passwordx TYPE bapipwdx                  ,

  w_return    TYPE bapiret2                  ,

  v_user      TYPE bapibname-bapibname       .

v_user = sy-uname.

*v_user = 'OMCRISTIANY'. "<<-- DOESN'T WORK

w_password-bapipwd = '$NEWPWD@123'.

w_passwordx-bapipwd = 'X'         .

CALL FUNCTION 'BAPI_USER_CHANGE'

  DESTINATION 'DEVCLNT300'

  EXPORTING

    username = v_user

    password = w_password

    passwordx = w_passwordx

  TABLES

    return   = t_return.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

LOOP AT t_return INTO w_return.

  WRITE: / w_return-message.

ENDLOOP.

Read only

0 Likes
6,243

Hi,

Modify your code as given below and check.


CALL FUNCTION 'BAPI_USER_CHANGE'  DESTINATION  'DEVCLNT300'  

EXPORTING 

username = v_user 

  password = w_password 

  passwordx = w_passwordx 

TABLES 

  return   = t_return.

Thanks

KH

Read only

0 Likes
6,243

I think you need to look at note 1287410. You may need to set the PRODUCTIVE_PASSWORD.parameter to 'X'.

Rob

Read only

Former Member
0 Likes
6,243

Try logging on with password

NEWPWD@123


If it works then it's the UpperCase/LowerCase flag in the FM

Read only

0 Likes
6,243

Thanks,

I tried in upper case and it didn't work either

Read only

RaymondGiuseppi
Active Contributor
0 Likes
6,243

What is your version, did you look for OSS notes, are you working with Central User Administration, and, if yes, are you changing password in a child system, and did you check CUA Customizing for redistribution (SCUM) ?

Regards,

Raymond

Read only

Former Member
0 Likes
6,244

Our basis is gonna disable CUA, and reset is gonna be made by web app using a RFC that I made.

That's the code:

obs: I'm gonna make some corrections such return after error in BAPI_USER_CHANGE


FUNCTION y_change_user_password.

*"----------------------------------------------------------------------

*"*"Interface local:

*"  IMPORTING

*"     VALUE(USUARIO) TYPE  BAPIBNAME-BAPIBNAME

*"     VALUE(SENHA) TYPE  BAPIPWD-BAPIPWD

*"     VALUE(DESBLOQUEIA) TYPE  CHAR1 OPTIONAL

*"  EXPORTING

*"     VALUE(T_RETORNO) TYPE  BAPIRET2_TAB

*"----------------------------------------------------------------------

  TYPE-POOLS: abap.

  DATA:

     w_pwd_reset   TYPE bapipwd    ,

     w_pwd_x       TYPE bapipwdx   ,

     w_retorno     TYPE bapiret2   ,

     v_usr_existe  TYPE i          ,

     v_usuario     TYPE string     ,

     v_senha_reset TYPE rsyst-bcode,

     v_usr_sap     TYPE sy-uname   ,

     v_nova_senha  TYPE rsyst-bcode,

     w_return      TYPE bapiret2   .

  v_usuario          = usuario          .

  w_pwd_reset-bapipwd = 'Mudar@123$reset'.                  "#EC NOTEXT

* Remove espaço no nome do usuário e senha

  CONDENSE v_usuario NO-GAPS.

* Dados de logon (utilização parte do núcleo)

  SELECT COUNT( DISTINCT bname )

   FROM usr02

   INTO v_usr_existe

   WHERE bname = usuario.

* Retorna erro se o usuário não existir

  IF v_usr_existe = 0.

    w_retorno-type   = 'W'                           .     "#EC NOTEXT

    w_retorno-message = 'Usuário & não existe no SAP' .     "#EC NOTEXT

    REPLACE '&' WITH v_usuario INTO w_retorno-message .

    APPEND w_retorno TO t_retorno                     .

  ELSE.

*   Verifica se é para desbloquear o usuário

    IF desbloqueia IS NOT INITIAL.

*     Desbloqueia o usuário

      CALL FUNCTION 'BAPI_USER_UNLOCK'

        EXPORTING

          username = usuario

        TABLES

          return  = t_retorno.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

    ENDIF.

*   Retorna erro se a senha não tiver sido informada

    IF senha IS INITIAL.

      w_retorno-type   = 'E'                                        . "#EC NOTEXT

      w_retorno-message = 'Senha não foi informada para o usuário & '. "#EC NOTEXT

      REPLACE '&' WITH v_usuario INTO w_retorno-message              .

      APPEND w_retorno TO t_retorno                                  .

    ELSE.

*     Reseta a senha do usuário

      w_pwd_x-bapipwd = abap_true.

      CALL FUNCTION 'BAPI_USER_CHANGE'

        EXPORTING

          username = usuario

          password = w_pwd_reset

          passwordx = w_pwd_x

        TABLES

          return   = t_retorno.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

      v_usr_sap    = usuario            .

      v_senha_reset = w_pwd_reset-bapipwd.

      v_nova_senha = senha              .

*     Altera a senha do usuário com a nova informada na RFC

      CALL FUNCTION 'SUSR_USER_CHANGE_PASSWORD_RFC'

        EXPORTING

          bname                    = v_usr_sap

          password                 = v_senha_reset

          new_password             = v_nova_senha

        IMPORTING

          return                   = w_return

        EXCEPTIONS

          change_not_allowed       = 1

          password_not_allowed     = 2

          internal_error           = 3

          canceled_by_user         = 4

          password_attempts_limited = 5

          OTHERS                   = 6.

      IF sy-subrc <> 0.

        w_retorno-type   = 'E'                          .  "#EC NOTEXT

        w_retorno-message = w_return-message             .  "#EC NOTEXT

        REPLACE '&' WITH v_usuario INTO w_retorno-message.

        APPEND w_retorno TO t_retorno                    .

      ENDIF.

    ENDIF.

  ENDIF.

ENDFUNCTION.