简体   繁体   中英

HttpClient 401 unauthorized at 1st, 3rd, 5th requests but successful at 2nd, 4th, 6th when calling ASP.NET Web API

I'm facing one issue when calling web api with httpclient (console program)

  • The requests failed (401, unauthorzied) at 1st, 3rd, 5th, etc requests
  • The requests succeed at 2nd, 4th, 6th , etc requests

Program.cs

static void Main(string[] args)
{
    var token = GlobalVariables.GetAccessToken();
    Console.WriteLine("================================");
    Console.WriteLine("TOKEN");
    Console.WriteLine($"Token : {token.Token}");
    Console.WriteLine($"Expires : {token.ExpiresIn}");
    Console.WriteLine("================================");
    Console.WriteLine("1");
    Console.WriteLine("================================");
    CallApiEndpoint(ApiUrl, token.Token);
    Console.WriteLine("================================");
    Console.WriteLine("2");
    Console.WriteLine("================================");
    CallApiEndpoint(ApiUrl, token.Token);
    Console.WriteLine("================================");
    Console.WriteLine("3");
    Console.WriteLine("================================");
    CallApiEndpoint(ApiUrl, token.Token);
    Console.WriteLine("================================");
    Console.WriteLine("4");
    Console.WriteLine("================================");
    CallApiEndpoint(ApiUrl, token.Token);
    Console.WriteLine("================================");
    Console.WriteLine("5");
    Console.WriteLine("================================");
    CallApiEndpoint(ApiUrl, token.Token);
    Console.WriteLine("================================");
    Console.WriteLine("6");
    Console.WriteLine("================================");
    CallApiEndpoint(ApiUrl, token.Token);
    Console.WriteLine("================================");

    Console.Read();
}

static void CallApiEndpoint(string url, string token)
{
    using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(BaseUrl);
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
        var response = httpClient.GetAsync(url).Result;

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Success");
        }
        else
        {
            Console.WriteLine(response.StatusCode);
        }
    }
}

Startup.cs

public void Configuration(IAppBuilder app)
{
    UserManagerFactory = () => new UserManager<User>();
    PublicClientId = "self";

    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
        AccessTokenExpireTimeSpan = TimeSpan.FromHours(1),
        AllowInsecureHttp = true
    };

    app.UseOAuthBearerTokens(OAuthOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
}

Response

响应

Thanks in advance,

After weeks of investigating this matter, I finally found out that the 401 unauthorized was due to Load Balancer on infrastructure level ( IIS ).

so I simply put MachineKey in web.config

Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM