目前,我正在像这样向我的请求添加一个授权头:
文件:SomeFile.cs
public interface ITestApi
{
[Get("/api/test/{id}")]
Task<string> GetTest([Header("Authorization")] string authorization, int id);
[Get("/api/tests/")]
Task<string> GetTests([Header("Authorization")] string authorization);
}
文件: SomeOtherFile.cs
var username = "xxx";
var password = "yyy";
var authHeader = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
var baseAddress = "https://my.test.net";
ITestApi myApi = RestService.For<ITestApi>(baseAddress);
int id = 123;
var test= myApi.GetTest(authHeader, id);
var tests = myApi.GetTests(authHeader);
有没有一种方法可以全局设置authHeader,以便我不必在每次调用时将其作为参数传递?(我正在使用Refit版本4.6.48)。换句话说,我想能够像这样进行调用:
是否有办法全局设置authHeader,使得在每个调用中都不需要将其作为参数传递?(我使用的是 Refit 版本 4.6.48)。换句话说,我想以如下方式进行调用:
var test= myApi.GetTest(id);
var tests = myApi.GetTests();
AuthorizationHeaderValueGetter
工作并不被忽略的接口上的属性。[Headers("Authorization: Basic")]
必须包括其中的: <scheme>
部分,以便Refit调用AuthorizationHeaderValueGetter
。 - jonfuller