HttpClient 401在第1次,第3次,第5次请求时未经授权,但在第2次,第4次,第6次调用ASP.NET Web API时成功

时间:2015-10-23 04:45:05

标签: c# asp.net-web-api oauth-2.0 owin

使用httpclient(控制台程序)调用web api时,我遇到了一个问题

  • 请求在第1,第3,第5等时失败(401,未授权) 请求
  • 请求在第2,第4,第6 等请求成功

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

提前致谢,

1 个答案:

答案 0 :(得分:0)

经过数周的调查,我终于发现基础架构级401 unauthorized Load Balancer IIS已归结为MachineKey

所以我只需将CLK放在web.config

希望它有所帮助。