C# 重新安装网络核心地址重新安装客户端读取响应头
我需要一个WPF.NETCore3.1应用程序的以下代码的帮助,该应用程序使用Refit来处理RESTAPI。我正在尝试从响应头获取AuthToken的值。但我找不到包含AuthorizationHeaderValueGetter值的属性 我确实看到了一些与此问题相关的bug-。据称它已在.net core 3.1版本中修复。但是我还不能检索到响应头 App.xaml.csC# 重新安装网络核心地址重新安装客户端读取响应头,c#,wpf,.net-core,refit,httpclientfactory,C#,Wpf,.net Core,Refit,Httpclientfactory,我需要一个WPF.NETCore3.1应用程序的以下代码的帮助,该应用程序使用Refit来处理RESTAPI。我正在尝试从响应头获取AuthToken的值。但我找不到包含AuthorizationHeaderValueGetter值的属性 我确实看到了一些与此问题相关的bug-。据称它已在.net core 3.1版本中修复。但是我还不能检索到响应头 App.xaml.cs private void ConfigureServices(IConfiguration configuration,
private void ConfigureServices(IConfiguration configuration, IServiceCollection services)
{
services.AddRefitClient<IService>(new RefitSettings()
{
AuthorizationHeaderValueGetter = () => Task.FromResult("AuthToken")
})
.ConfigureHttpClient(c => c.BaseAddress = new
Uri(Configuration.GetSection("MyConfig:GatewayService").Value));
}
专用void配置服务(IConfiguration配置、iSeries收集服务)
{
services.AddRefitClient(新的重新安装设置()
{
AuthorizationHeaderValueGetter=()=>Task.FromResult(“AuthToken”)
})
.ConfigureHttpClient(c=>c.BaseAddress=new
Uri(Configuration.GetSection(“MyConfig:GatewayService”).Value);
}
IService.cs
接口IService的定义如下:
[Headers("Content-Type: application/json")]
public interface IService
{
[Post("/v1/Authtoken/")]
public Task<string> Authenticate([Body] Authenticate payload);
}
[标题(“内容类型:application/json”)]
公共接口设备
{
[发布(“/v1/Authtoken/”)
公共任务验证([Body]验证有效负载);
}
我正在我的ViewModel(WPF)中注入IService,并试图获取“AuthToken”头的值,该头应该已经设置好
视图模型
public class SomeViewModel: ISomeViewModel
{
public SomeViewModel(IService service)
{
this.Service = service;
}
public async Task<Tuple<bool, string>> Somemethod()
{
var authResponse = await Service.Authenticate(authPayload);
.......
}
}
公共类SomeViewModel:ISomeViewModel
{
公共视图模型(iSeries服务)
{
服务=服务;
}
公共异步任务方法()
{
var authResponse=wait Service.Authenticate(authPayload);
.......
}
}
我设法获得了响应标题。服务的返回类型必须更改为System.Net.Http.HttpResponseMessage
[Headers("Content-Type: application/json")]
public interface IService
{
[Post("/v1/Authtoken/")]
public Task<HttpResponseMessage> Authenticate([Body] Authenticate payload);
}
public static class RefitExtensions
{
public static async Task<string>GetAuthToken(this Task<HttpResponseMessage> task)
{
var response = await task.ConfigureAwait(false);
string authToken = response.Headers.GetValues("AuthToken").FirstOrDefault();
return await Task.FromResult(authToken);
}
}
var authToken = await Service.Authenticate(authPayload).GetAuthToken();