Ai
6 Star 88 Fork 35

dotNET China/HttpAgent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
AuthorizationDelegatingHandler.cs 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
namespace HttpAgent.Samples;
public class AuthorizationDelegatingHandler : DelegatingHandler
{
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
// 参考 SendAsync 代码
return base.Send(request, cancellationToken);
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
// 添加 JWT (JSON Web Token) 身份验证
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "your token");
// 添加 Basic 身份验证
var base64Credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("username" + ":" + "password"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
// 添加自定义 Schema 身份验证
request.Headers.Authorization = new AuthenticationHeaderValue("X-Token", "your token");
return base.SendAsync(request, cancellationToken);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/dotnetchina/HttpAgent.git
git@gitee.com:dotnetchina/HttpAgent.git
dotnetchina
HttpAgent
HttpAgent
master

搜索帮助