本文整理汇总了C#中HttpClient.SetBasicAuthentication方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.SetBasicAuthentication方法的具体用法?C# HttpClient.SetBasicAuthentication怎么用?C# HttpClient.SetBasicAuthentication使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpClient
的用法示例。
在下文中一共展示了HttpClient.SetBasicAuthentication方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var handler = new WebRequestHandler();
handler.ClientCertificates.Add(
X509.CurrentUser
.My
.SubjectDistinguishedName
.Find("CN=client")
.First());
//var handler = new HttpClientHandler
//{
// ClientCertificateOptions = ClientCertificateOption.Automatic
//};
var client = new HttpClient(handler)
{
BaseAddress = new Uri("https://web.local:444/api/")
};
client.SetBasicAuthentication("bob", "bob");
var result = client.GetStringAsync("identity").Result;
Console.WriteLine(JArray.Parse(result));
}
示例2: GetToken
private static string GetToken()
{
var client = new HttpClient();
client.SetBasicAuthentication("dominick", "abc!123");
// SAML11
//var response = client.GetAsync("https://idsrv.local/issue/simple?realm=urn:testrp&tokenType=saml11").Result;
// SAML2
var response = client.GetAsync("https://idsrv.local/issue/simple?realm=urn:testrp&tokenType=saml2").Result;
response.EnsureSuccessStatusCode();
var tokenResponse = response.Content.ReadAsStringAsync().Result;
var json = JObject.Parse(tokenResponse);
return json["access_token"].ToString();
}
示例3: GetToken
private static string GetToken()
{
"Requesting token".ConsoleYellow();
var client = new HttpClient
{
BaseAddress = new Uri(simpleHttpEndpoint)
};
client.SetBasicAuthentication("bob", "abc!123");
var response = client.GetAsync("?realm=http://sbserver/swttest/&tokentype=swt").Result;
response.EnsureSuccessStatusCode();
var tokenResponse = response.Content.ReadAsStringAsync().Result;
var token = JObject.Parse(tokenResponse)["access_token"].ToString();
Console.WriteLine(token);
return token;
}
示例4: RevokeToken
//static TokenResponse GetClientToken()
//{
// var client = new OAuth2Client(
// new Uri("https://HFL0100:44333/connect/token"),
// "silicon",
// "F621F470-9731-4A25-80EF-67A6F7C5F4B8");
// ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
// return client.RequestClientCredentialsAsync("api1").Result;
//}
static TokenResponse RevokeToken(string token)
{
//var client = new OAuth2Client(
// new Uri("https://HFL0100:44333/connect/revocation"),
// "carbon",
// "21B5F798-BE55-42BC-8AA8-0025B903DC3B");
//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
//var a = new System.Collections.Generic.Dictionary<string, string>();
//a.Add("token", token);
//a.Add("token_type_hint", "access_token");
//return client. (a).Result;
var client = new HttpClient();
client.SetBasicAuthentication("carbon", "21B5F798-BE55-42BC-8AA8-0025B903DC3B");
var postBody = new System.Collections.Generic.Dictionary<string, string>
{
{ "token", token },
//{ "token_type_hint", "refresh_token" }
{ "token_type_hint", "access_token" }
};
var result = client.PostAsync("https://HFL0100:44333/connect/revocation", new FormUrlEncodedContent(postBody)).Result;
return null;
}