Jump to content

TRESTClient + IIS + ISAPI DLL not working, Windows service does


Masteritec

Recommended Posts

 

The following code in pas unit runs fine in windows service but not in IIS+ISAPI DLL, any solution:

  LrestClient:= TRESTClient.Create(LstrURL);
  try
    LrestRequest:= TRESTRequest.Create(nil);
    try
      LrestResponce:= TRESTResponse.Create(nil);
      try
        LrestRequest.Client:= LrestClient;
        LrestRequest.Response:= LrestResponce;
        LrestRequest.Method:= rmPOST;
        LrestRequest.AssignedValues:= [REST.Client.TRESTRequest.TAssignedValue.rvConnectTimeout,
            REST.Client.TRESTRequest.TAssignedValue.rvReadTimeout];

        LrestRequest.ReadTimeout:= cstTimeOut;

        LrestRequest.Params.Clear;
        LrestRequest.AddParameter('Content-Type', 'application/x-www-form-urlencoded', pkGETorPOST);
        LrestRequest.AddParameter('client_id', LstrClientID, pkGETorPOST);
        LrestRequest.AddParameter('client_secret', LstrClientSecret1, pkGETorPOST);
        LrestRequest.AddParameter('grant_type', 'client_credentials', pkGETorPOST);
        LrestRequest.AddParameter('scope', 'InvoicingAPI', pkGETorPOST);
        LrestRequest.Execute;
      finally
        LrestResponce.Free;
      end;
    finally
      LrestRequest.Free
    end;
  finally
    LrestClient.Free;
  end;

Link to comment
Share on other sites

MasterWebAccountingV8.dll: 00000E64: 10:21:49 []:>--------------------------------------------------------------<
MasterWebAccountingV8.dll: 00000E64: 10:21:49 []:Starting Server. Module Handle: 0000000000400000
MasterWebAccountingV8.dll: 00000E64: 10:21:49 []:Server Started. Module Handle: 0000000000400000
MasterWebAccountingV8.dll: 000014A8: 10:24:10 []:Terminating ISAPI Server. Module Handle: 0000000000400000
MasterWebAccountingV8.dll: 000014A8: 10:24:10 []:ISAPI Server Terminated. Module Handle: 0000000000400000
MasterWebAccountingV8.dll: 000014A8: 10:24:10 []:<-------------------------------------------------------------->

attached file is from C:\inetpub\logs\LogFiles\W3SVC1

u_ex240613.log

Link to comment
Share on other sites

HI Masteritec,

i use in UniGui:

procedure TMainForm.UniFormCreate( Sender : TObject );
  var cookie1, cookie2 : String; anwendung : String; farbe_from, farbe_to : String;

    // -----------
    ergebnis : String; LJsonObj : TJSONObject; LJsonValue : TJSONValue;
    // -------
    ini : TIniFile; FileName : String; state : string; CallBackFrom, AuthCode, token : string; http : TNetHTTPClient; IdHTTP : TIdHTTP; IdSSLIOHandlerSocketOpenSSL : TIdSSLIOHandlerSocketOpenSSL; Params : TStringList; Lresponse : IHTTPresponse;
    oauth_token, oauth_verifier, OAuth_secret, aMsg : string;
    function GetSimpleValue( Jstring, Key : string ) : string;
      var jv, fv : TJSONValue;
      begin
        Result := '';
        jv := TJSONObject.ParseJSONValue( Jstring );
        fv := jv.FindValue( Key );
        if fv <> nil
        then
          Result := fv.ToString;
      end;

  begin

    UniPageControl1.ActivePage := UniTabSheet1;

    FormatSettings.CurrencyFormat := 3;
    FormatSettings.NegCurrFormat := 8;
    FormatSettings.ThousandSeparator := '.';
    FormatSettings.DecimalSeparator := ',';
    FormatSettings.CurrencyDecimals := 2;
    FormatSettings.DateSeparator := '.';
    FormatSettings.ShortDateFormat := 'dd.MM.yyyy';
    FormatSettings.LongDateFormat := 'dddd, d. MMMM yyyy';
    FormatSettings.TimeSeparator := ':';
    FormatSettings.TimeAMString := '';
    FormatSettings.TimePMString := '';
    FormatSettings.ShortTimeFormat := 'hh:mm';
    FormatSettings.LongTimeFormat := 'hh:mm:ss';

    // zusatz Start
    UniMainModule.AUserName := '';

    FileName := UniServerModule.StartPath + 'sso_oauth2.ini';

    ini := TIniFile.Create( FileName );
    try
      UniMainModule.C_client_id_Az := ini.ReadString( 'oauth2', 'C_client_id_Az', '' );

      UniMainModule.C_redirect_uri_Az := ini.ReadString( 'oauth2', 'C_redirect_uri_Az', '' );

      UniMainModule.C_tenant_Az := ini.ReadString( 'oauth2', 'C_tenant_Az', '' );

      UniMainModule.C_State_Az := ini.ReadString( 'oauth2', 'C_State_Az', '' );
 
      UniMainModule.C_scope_Az := ini.ReadString( 'oauth2', 'C_scope_Az', '' );

      UniMainModule.C_client_secret_Az := ini.ReadString( 'oauth2', 'C_client_secret_Az', '' );

    finally
      ini.Free;
    end;

    CallBackFrom := uniGUIApplication.UniApplication.parameters.Values[ 'oauth2callback' ];
    if CallBackFrom = ''
    then
      Exit;
    UniMainModule.AzureState := '123456';

    UniSession.Log( 'Mainmodule beforeLogin.Params:' + uniGUIApplication.UniApplication.parameters.text );

    UniMainModule.AzureAuthCode := uniGUIApplication.UniApplication.parameters.Values[ 'code' ];
    state := uniGUIApplication.UniApplication.parameters.Values[ 'state' ];
    CallBackFrom := uniGUIApplication.UniApplication.parameters.Values[ 'oauth2callback' ];
    UniSession.Log( 'Mainmodule beforeLogin.Params:' + UniMainModule.AzureState );

    // ----------------

    UniSession.Log( 'Parameter für https://login.microsoftonline.com/organizations/oauth2/v2.0/token...' );
    UniSession.Log( 'code=' + UniMainModule.AzureAuthCode );
    UniSession.Log( 'client_id=' + UniMainModule.C_client_id_Az );
    UniSession.Log( 'client_secret=' + UniMainModule.C_client_secret_Az );
    UniSession.Log( 'scope=' + UniMainModule.C_scope_Az );
    UniSession.Log( 'redirect_uri=' + UniMainModule.C_redirect_uri_Az );
    UniSession.Log( 'grant_type=authorization_code' );
    // ----------------

    // This is for Azure
    token := '';
    if ( CallBackFrom = 'azure' )
    then
    begin
      // yes call from azure, so get access token
      try
        try
          UniSession.Log( ' --> Got Code - see if we can get an token...' );

          Params := TStringList.Create;
          Params.add( 'code=' + UniMainModule.AzureAuthCode );
          Params.add( 'client_id=' + UniMainModule.C_client_id_Az );
          Params.add( 'client_secret=' + UniMainModule.C_client_secret_Az );
          Params.add( 'scope=' + UniMainModule.C_scope_Az );
          Params.add( 'redirect_uri=' + UniMainModule.C_redirect_uri_Az );
          Params.add( 'grant_type=authorization_code' );

          http := TNetHTTPClient.Create( Self );
          http.SecureProtocols := [ THTTPSecureProtocol.SSL3, THTTPSecureProtocol.TLS1, THTTPSecureProtocol.TLS11, THTTPSecureProtocol.TLS12, THTTPSecureProtocol.TLS13 ];

          Lresponse := http.post( 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token', Params );
          if Lresponse.StatusText <> 'xOK'
          then
          begin
            login_label.Caption := '......Azure Sign in';
            token := GetSimpleValue( Lresponse.ContentAsString, 'access_token' );
            UniMainModule.GToken := stringreplace( token, '"', '', [ rfreplaceall ] );;
            UniSession.Log( Lresponse.ContentAsString );
            aMsg := Lresponse.ContentAsString;

            Lresponse := http.get( 'https://graph.microsoft.com/v1.0/me', nil, [ TNetHeader.Create( 'Authorization', 'Bearer ' + UniMainModule.GToken ) ] );
            if stringreplace( GetSimpleValue( Lresponse.ContentAsString, 'mail' ), '"', '', [ rfreplaceall ] ) <> '' then
            begin
              UniSession.Log( Lresponse.ContentAsString );
              UniMainModule.AUserName := stringreplace( GetSimpleValue( Lresponse.ContentAsString, 'mail' ), '"', '', [ rfreplaceall ] );
              UniMainModule.LoginType := 2;

              login_label.Caption := '......Azure Sign in for user' + #13 + UniMainModule.AUserName;
            end;

          end;

          if token <> ''
          then
          begin

            UniSession.Log( 'Habe Token erhalten: ' + token );

          end;

        Except
          on E : exception do
          begin
            UniMainModule.LoginType := 0;
            UniSession.Log( 'LAST ERROR: ' + SysErrorMessage( GetLastError ) );
            UniSession.Log( 'MESSAGE ERROR: ' + E.Message );
          end;
        end;

      finally
        http.Free;
        Params.Free;

      end;
    end;

    // --------------------

  end;

 

Login

procedure TMainForm.UniButton1Click( Sender : TObject );
  var LURL : String;
  begin

    if ( UniMainModule.AUserName <> '' ) AND ( UniMainModule.AUserName <> 'null' )
    then
    begin
      Azure_login;
    end else begin
      sleep(1000);
      LURL := 'https://login.microsoftonline.com/' + UniMainModule.C_tenant_Az + '/oauth2/v2.0/authorize';
      LURL := LURL + '?response_type=' + URIEncode( 'code' );
      LURL := LURL + '&client_id=' + URIEncode( UniMainModule.C_client_id_Az );
      LURL := LURL + '&redirect_uri=' + URIEncode( UniMainModule.C_redirect_uri_Az );
      LURL := LURL + '&response_mode=' + URIEncode( 'query' );
      LURL := LURL + '&scope=' + UniMainModule.C_scope_Az;
      LURL := LURL + '&state=' + URIEncode( UniMainModule.C_State_Az );
      UniSession.UrlRedirect( LURL );
    end;

  end;

Logout

procedure TMainForm.UniButton3Click( Sender : TObject );
  var url : String;
  begin
    url := 'https://login.microsoftonline.com/organization/oauth2/logout';
    UniSession.UrlRedirect( url );
  end;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...