系统相关
首页 > 系统相关> > WebApi 模拟Windows认证

WebApi 模拟Windows认证

作者:互联网

1.使用当前登录用户

HttpClientHandler hch = new HttpClientHandler();

hch.UseDefaultCredentials = true;  

2.使用账户密码,模拟任何账户

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("username", "password", "demain");
using (HttpClient client = new HttpClient(handler))
{
HttpResponseMessage response = client.GetAsync("http://localhost/webapi/api/demo").Result;
IEnumerable<string> userNames = response.Content.ReadAsAsync<IEnumerable<string>>().Result;
foreach (string userName in userNames)
{
Console.WriteLine(userName);
}
}
Console.Read();

标签:WebApi,Console,Windows,认证,HttpClientHandler,handler,new,hch,HttpClient
来源: https://www.cnblogs.com/JerryZhang320/p/15734772.html