我想使用Rabobank API来获取有关我的银行帐户的信息并将其显示在我的应用程序中。我已经在门户中创建了一个应用程序,并将其订阅了所需的API。我在调用API时遇到问题。
我在dotnet core 3.0中注入了HttpClient。
services.AddHttpClient<RabobankService>()
.ConfigurePrimaryHttpMessageHandler(sp => sp.GetRequiredService<RabobankHttpClientHandler>());
在构造函数中,设置标题:
public class RabobankService
{
public IConfiguration Configuration { get; }
public HttpClient Client { get; }
public IHttpContextAccessor HttpContextAccessor { get; }
public RabobankService(IConfiguration configuration, HttpClient client, IHttpContextAccessor httpContextAccessor)
{
Configuration = configuration;
HttpContextAccessor = httpContextAccessor;
SetProperties(client);
Client = client;
}
private void SetProperties(HttpClient client)
{
IConfigurationSection configSection = Configuration.GetSection("Authentication:Rabobank");
IConfigurationSection apiUrlSection = Configuration.GetSection("ApiUrls");
var clientId = configSection["ClientId"];
var clientSecret = configSection["ClientSecret"];
byte[] encodedBytes = System.Text.Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}");
string base64encodedData = Convert.ToBase64String(encodedBytes);
client.BaseAddress = new Uri(apiUrlSection[ClientConstants.RabobankBaseUrl]);
var hasAuthHeader = HttpContextAccessor.HttpContext.Request.Headers.ContainsKey("Banking-Authorization");
var authHeader = HttpContextAccessor.HttpContext.Request.Headers["Banking-Authorization"];
client.DefaultRequestHeaders.Authorization =
// If resource call set header
hasAuthHeader ?
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authHeader[0]) :
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $"Basic {base64encodedData}");
client.DefaultRequestHeaders.Add("X-IBM-Client-Id", clientId);
var cert = new X509Certificate2("cert.cert");
var certPublicKey = Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks);
client.DefaultRequestHeaders.Add("TPP-Signature-Certificate", certPublicKey.Replace(System.Environment.NewLine, string.Empty));
client.DefaultRequestHeaders.Add("Digest", Guid.NewGuid().ToString());
client.DefaultRequestHeaders.Add("Signature", Guid.NewGuid().ToString());
}
在处理程序中,我在构造函数中设置了它:
public class RabobankHttpClientHandler : HttpClientHandler
{
public RabobankHttpClientHandler()
{
ClientCertificateOptions = ClientCertificateOption.Manual;
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
ClientCertificates.Add(new X509Certificate2("cert.cert"));
}
}
然后我这样称呼端点:
public async Task<Account> GetAccount()
{
var request = new HttpRequestMessage (HttpMethod.Get, "payments/account-information/ais/v3/accounts");
return await ExecuteRequest<Account>(request);
}
private async Task<T> ExecuteRequest<T>(HttpRequestMessage request, bool deserialize = true)
{
try
{
Client.DefaultRequestHeaders.Add("X-Request-ID", Guid.NewGuid().ToString());
Client.DefaultRequestHeaders.Add("Date", DateTime.Now.ToUniversalTime().ToString("r"));
var response = await Client.SendAsync(request);
var content = response.Content;
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
else
{
var result = await response.Content.ReadAsStringAsync();
var errorResponse = JsonConvert.DeserializeObject<BankingErrorResponse>(result);
if(response.StatusCode == System.Net.HttpStatusCode.Unauthorized && errorResponse.MoreInformation != ClientConstants.NotUsingMutualTls)
{
throw new BankingUnauthorizedException();
}
else
{
throw new Exception(errorResponse.MoreInformation);
}
}
}
catch(BankingUnauthorizedException)
{
HttpContextAccessor.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
return default(T);
}
catch(Exception ex)
{
throw ex;
}
}
我收到一个401响应,错误为:应用程序未使用双向TLS。
有人可以帮我吗?