本文整理汇总了C#中System.Net.Http.HttpClient.SetBearerToken方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.SetBearerToken方法的具体用法?C# HttpClient.SetBearerToken怎么用?C# HttpClient.SetBearerToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.SetBearerToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var tokenClient = new TokenClient(
"http://localhost:18942/connect/token",
"test",
"secret");
//This responds with the token for the "api" scope, based on the username/password above
var response = tokenClient.RequestClientCredentialsAsync("api1").Result;
//Test area to show api/values is protected
//Should return that the request is unauthorized
try
{
var unTokenedClient = new HttpClient();
var unTokenedClientResponse = unTokenedClient.GetAsync("http://localhost:19806/api/values").Result;
Console.WriteLine("Un-tokened response: {0}", unTokenedClientResponse.StatusCode);
}
catch (Exception ex)
{
Console.WriteLine("Exception of: {0} while calling api without token.", ex.Message);
}
//Now we make the same request with the token received by the auth service.
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
var apiResponse = client.GetAsync("http://localhost:19806/identity").Result;
var callApiResponse = client.GetAsync("http://localhost:19806/api/values").Result;
Console.WriteLine("Tokened response: {0}", callApiResponse.StatusCode);
Console.WriteLine(callApiResponse.Content.ReadAsStringAsync().Result);
Console.Read();
}
示例2: CallApi
/// <summary>
/// Chama a api do AppService com um token valido.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
private async Task<string> CallApi(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var json = await client.GetStringAsync("https://localhost:44303/identity");
return JArray.Parse(json).ToString();
}
示例3: CallApi
private static string CallApi(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var result = client.GetStringAsync(ApplicationConstants.UrlBaseApi + "/api/test").Result;
return result;
}
示例4: GetClient
public static HttpClient GetClient(string requestedVersion = null)
{
CheckAndPossiblyRefreshToken((HttpContext.Current.User.Identity as ClaimsIdentity));
HttpClient client = new HttpClient();
var token = (HttpContext.Current.User.Identity as ClaimsIdentity).FindFirst("access_token");
if (token != null)
{
client.SetBearerToken(token.Value);
}
client.BaseAddress = new Uri(ExpenseTrackerConstants.ExpenseTrackerAPI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
if (requestedVersion != null)
{
// through a custom request header
//client.DefaultRequestHeaders.Add("api-version", requestedVersion);
// through content negotiation
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/vnd.expensetrackerapi.v"
+ requestedVersion + "+json"));
}
return client;
}
示例5: CallApi
public async Task<IActionResult> CallApi()
{
var accessToken = User.Claims.SingleOrDefault(claim => claim.Type == "access_token");
if (accessToken == null)
{
return new BadRequestResult();
}
HttpClient client = new HttpClient();
// The Resource API will validate this access token
client.SetBearerToken(accessToken.Value);
client.BaseAddress = new Uri(Constants.ResourceApi);
HttpResponseMessage response = await client.GetAsync("api/Tickets/Read");
if (!response.IsSuccessStatusCode)
{
return new BadRequestObjectResult("Error connecting with the API");
}
var responseContent = await response.Content.ReadAsStringAsync();
return new ObjectResult(responseContent);
}
示例6: CallApi
static void CallApi(TokenResponse response)
{
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
Console.WriteLine(client.GetStringAsync("http://localhost:44334/test").Result);
}
示例7: GetClient
public static HttpClient GetClient()
{
if (currentClient == null)
{
var accessToken = App.ExpenseTrackerIdentity.Claims.First
(c => c.Name == "access_token").Value;
currentClient = new HttpClient(new Marvin.HttpCache.HttpCacheHandler()
{
InnerHandler = new HttpClientHandler()
});
currentClient.SetBearerToken(accessToken);
currentClient.BaseAddress = new Uri(ExpenseTrackerConstants.ExpenseTrackerAPI);
currentClient.DefaultRequestHeaders.Accept.Clear();
currentClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
return currentClient;
}
示例8: CallApi
static String CallApi(TokenResponse response)
{
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
return client.GetStringAsync(Constants.ApiUrl + "/test").Result;
}
示例9: CallApi
private async Task<string> CallApi(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var json = await client.GetStringAsync("http://localhost:32227/api/values");
return json;
}
示例10: CallApi
protected async Task<string> CallApi(string token, string url)
{
var client = new HttpClient();
client.SetBearerToken(token);
var json = await client.GetStringAsync(url);
return JArray.Parse(json).ToString();
}
示例11: CallApi
private async Task<string> CallApi(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var json = await client.GetStringAsync(IdConstants.IdHost + "identity");
return JArray.Parse(json).ToString();
}
示例12: GetUserInfos
private async Task<String> GetUserInfos()
{
var user = User as ClaimsPrincipal;
var token = user.FindFirst("access_token").Value;
var client = new HttpClient();
client.SetBearerToken(token);
HttpResponseMessage response = await client.GetAsync("https://localhost:44333/core/connect/userinfo");
return await response.Content.ReadAsStringAsync();
}
示例13: CallApi
/// <summary>
/// Get a token before making a GET request to the specified api path
/// </summary>
/// <param name="route"></param>
/// <returns></returns>
public static string CallApi(string path)
{
string token = GetToken();
//string token = GetTokenExplained();
var client = new HttpClient();
client.SetBearerToken(token);
return client.GetStringAsync(ConfigurationManager.AppSettings["Api"] + "/" + path).Result;
}
示例14: Main
static void Main(string[] args)
{
var response = GetToken();
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
var result = client.GetStringAsync("http://localhost:2025/claims").Result;
Console.WriteLine(JArray.Parse(result));
}
示例15: CallApiAsync
private async Task<JArray> CallApiAsync(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var response = await client.GetAsync("https://localhost:44347/test");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JArray.Parse(content);
}