2

Maximo Rest API にアクセスするアプリケーションを作成しています。これは、API を呼び出すために使用するコードです。

.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;
        }
    }

}

ご覧のとおり、Cookie コンテナーを使用して、認証時に応答と共に送信される Cookie をキャプチャしています。Authenticate() は動作します。LTPA トークンを含む 302 を返します。しかし、GET 要求は失敗します。次のエラーが表示されます。

ログインに使用される LTPA トークンが無効です。WebSphere Application Server Security が有効になっている場合、LTPA トークンはログイン・プロセスに使用されます。数秒待ってから、再度ログインしてください。問題が解決しない場合は、ブラウザの Cookie をクリアするか、ブラウザを再起動してください。

Postman を使用しているときに同様のエラーが発生しますが、リクエストを再試行すると機能します。

フレームワークを切り替えて、.NET Core 2.1 を使用しました。まったく同じコードが問題なく機能しました。

これが .NET Framework ではなく .NET Core で機能するのはなぜですか? .NET コアを使用しても問題は解決しません。.NET 4.5 である必要があります。誰でもこれで私を助けることができますか?

4

0 に答える 0