I'm facing one issue when calling web api with httpclient (console program)
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.