The LTPA token that is used to login is invalid - Maximo Rest API

470 Views Asked by At

I'm building an application that accesses Maximo Rest API, and I this is the code I use to call the API.

I'm using .NET Framework 4.5 C#

public class MaximoClient
{

    private string ServerURL;
    private string AuthUsername;
    private string AuthPassword;
    private CookieContainer cookieContainer = null;

    private string HttpGetRequest(string URL)
    {
        string ResponseContent;
        try
        {
            if (this.cookieContainer == null)
            {
                this.Authenticate();
            }


            HttpClientHandler handler = new HttpClientHandler
            {
                CookieContainer = this.cookieContainer
            };
            HttpClient client = new HttpClient(handler);

            HttpRequestMessage Request = new HttpRequestMessage
            {
                RequestUri = new Uri(URL, UriKind.Absolute),
                Method = HttpMethod.Get
            };


            var Response = client.SendAsync(Request).Result;
            ResponseContent = Response.Content.ReadAsStringAsync().Result;
        }
        catch (Exception e)
        {
            throw e;
        }

        return ResponseContent;
    }



    private void Authenticate()
    {
        try
        {
            this.cookieContainer = new CookieContainer();
            HttpClientHandler handler = new HttpClientHandler
            {
                CookieContainer = cookieContainer,
                AllowAutoRedirect = false,
                UseCookies = true,

            };

            this.Client = new HttpClient(handler);

            HttpClient client = this.Client;

            HttpRequestMessage Request = new HttpRequestMessage
            {
                RequestUri = new Uri($"{this.ServerURL}/oslc/j_security_check", UriKind.Absolute),
                Method = HttpMethod.Post
            };
            var postData = Encoding.ASCII.GetBytes($"j_username={this.AuthUsername}&j_password={this.AuthPassword}");
            Request.Content = new ByteArrayContent(postData);
            Request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");


            HttpResponseMessage Response = client.SendAsync(Request).Result;

            string ResponseContent = Response.Content.ReadAsStringAsync().Result;

            int code = (int)Response.StatusCode;
            if (code > 399)
            {
                throw new Exception("HttpWebRequest returned Status Code:" + Response.StatusCode + " : " + Response.ReasonPhrase);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }

}

As you can see I'm using the cookie container to capture cookies sent with the response when I authenticate. Authenticate() works. It returns a 302, with an LTPA token. But the GET request fails. I get this error:

The LTPA token that is used to login is invalid. LTPA tokens are used for the login process when WebSphere Application Server Security is enabled. Wait a few seconds and then try again to log in. If the problem persists, clear your browser cookies or restart the browser

I get a similar error when I'm using Postman but when I try the request again, it works.

I switched Frameworks and used .NET Core 2.1. The exact same code worked without any issues.

Why does this work for .NET Core and not .NET framework? Using .NET core is not a solution to my problem, it needs to be .NET 4.5. Can anyone help me with this?

0

There are 0 best solutions below