本文整理汇总了C#中HttpClient.SetBearerToken方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.SetBearerToken方法的具体用法?C# HttpClient.SetBearerToken怎么用?C# HttpClient.SetBearerToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpClient
的用法示例。
在下文中一共展示了HttpClient.SetBearerToken方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CallApi
static void CallApi(TokenResponse response)
{
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
Console.WriteLine(client.GetStringAsync("http://localhost:14869/test").Result);
}
示例2: CallService
private static string CallService(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var response = client.GetStringAsync(new Uri("http://localhost:2727/api/identity")).Result;
return response;
}
示例3: CallApi
static string CallApi(TokenResponse token, string endpoint)
{
Console.WriteLine(token.AccessToken);
var client = new HttpClient();
client.SetBearerToken(token.AccessToken);
return client.GetStringAsync(endpoint).Result;
}
示例4: CallService
private static void CallService(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var response = client.GetStringAsync(new Uri("http://localhost:2727/api/identity")).Result;
Console.WriteLine(response);
}
示例5: CallService
private static void CallService(string token)
{
var client = new HttpClient();
client.SetBearerToken(Convert.ToBase64String(Encoding.UTF8.GetBytes(token)));
var response = client.GetAsync("http://localhost:2727/api/identity").Result;
response.EnsureSuccessStatusCode();
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}
示例6: CallApi
static void CallApi(TokenResponse response)
{
var client = new HttpClient();
client.SetBearerToken(response.AccessToken);
Console.WriteLine("Calling api...");
var result = client.GetStringAsync("http://localhost:47922/test").Result;
Console.WriteLine("API called...");
Console.WriteLine(result);
//WebClient client = new WebClient();
//client.Headers["Authorization"] = "Bearer " + response.AccessToken;
//var result = client.DownloadString("http://localhost:47922/test");
//Console.WriteLine("Result: " + result);
}
示例7: Configure
public void Configure(IApplicationBuilder app)
{
app.UseCookieAuthentication(options =>
{
options.AuthenticationScheme = "Cookies";
options.AutomaticAuthentication = true;
});
app.Map("/code", application =>
{
application.Use((context, next) =>
{
var client = new OAuth2Client(new Uri("http://localhost:5000/core/connect/authorize"));
var url = client.CreateCodeFlowUrl(clientId: "0011FF", redirectUri: "http://localhost:5001/callback", scope: "read write");
context.Response.Redirect(url);
return Task.FromResult(0);
});
});
app.Map("/callback", application =>
{
application.Use(next => async context =>
{
var client = new OAuth2Client(new Uri("http://localhost:5000/core/connect/token"), "0011FF", "ABCDEFG");
var code = context.Request.Query["code"];
TokenResponse response = await client.RequestAuthorizationCodeAsync(code, "http://localhost:5001/callback");
if (!string.IsNullOrEmpty(response.AccessToken))
{
List<Claim> claims = new List<Claim>();
claims.Add(new Claim("access_token", response.AccessToken));
claims.Add(new Claim("expires_at", (DateTime.UtcNow.ToEpochTime() + response.ExpiresIn).ToDateTimeFromEpoch().ToString()));
ClaimsIdentity id = new ClaimsIdentity(claims, "cookie");
ClaimsPrincipal principal = new ClaimsPrincipal(id);
context.Response.SignIn("Cookies", principal);
}
});
});
app.Map("/info", application =>
{
application.Use(next => async context =>
{
ClaimsPrincipal principal = context.User;
await context.Response.WriteAsync(principal.FindFirst("access_token").Value);
});
});
app.Map("/call", application =>
{
application.Use(next => async context =>
{
HttpClient client = new HttpClient();
string accessToken = context.User.FindFirst("access_token").Value;
client.SetBearerToken(accessToken);
string response = await client.GetStringAsync("http://localhost:5002/action");
await context.Response.WriteAsync(response);
});
});
}