Repository navigation
Login on another page/server #1561
Closed
sergioocode
started this conversation in
General
Replies: 1 comment
|
Yes, this works with Refit. The key points:
var cookies = new CookieContainer();
Func<HttpMessageHandler> handlerFactory = () =>
{
var handler = new HttpClientHandler { CookieContainer = cookies, UseCookies = true };
handler.ClientCertificates.Add(DigitalCert.GetCert(context.Configuration));
return handler;
};
services.AddRefitClient<ILoginApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://serverlogin.com/"))
.ConfigurePrimaryHttpMessageHandler(handlerFactory);
services.AddRefitClient<IUsersApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://server2.com/cve_cgi/api/users/"))
.ConfigurePrimaryHttpMessageHandler(handlerFactory);
public interface ILoginApi
{
[Get("/cgi_AUT2000/CAutInicio.cgi")]
Task Connect([Query] string reference);
}
public interface IUsersApi
{
[Get("")]
Task<UserInfo> GetInfo();
}Call Notes:
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hello, in this moment i am using "IHttpClientFactory" and all works fine.
I need to know if is possible this approach:
private const string clientName = "SOC";-login auth (with cert x509)
public async Task Connect(string url) { HttpClient httpclient = clientFactory.CreateClient(clientName); var res = await httpclient!.GetAsync( Path.Combine( string.Format( "https://serverlogin.com/cgi_AUT2000/CAutInicio.cgi?reference={0}", url ) ) ); }-Get info
public async Task GetInfo(HttpRequestMessage message) { message.RequestUri = new Uri("https://server2.com/cve_cgi/api/users/"); HttpClient httpclient = clientFactory.CreateClient(clientName); HttpResponseMessage? res = await httpclient!.SendAsync(message); }This is DI:
services .AddHttpClient( "SOC", c => { c.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0"); } ) .ConfigurePrimaryHttpMessageHandler(() => { CookieContainer? cookieContainer = new(); HttpClientHandler? handler = new() { CookieContainer = cookieContainer }; handler.ClientCertificates.Add( DigitalCert.GetCert(context.Configuration) ); return handler; });This methods works fine.
So, now i want use the Refit library. Is this possible?
I have problems because i dont knows how change the base address...
services .AddRefitClient<IInterface>() .ConfigureHttpClient(c => { c.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0"); c.BaseAddress = new Uri("https://server2.com/cve_cgi/api/users/"); }) .ConfigurePrimaryHttpMessageHandler(() => { CookieContainer? cookieContainer = new(); HttpClientHandler? handler = new() { CookieContainer = cookieContainer }; handler.ClientCertificates.Add( DigitalCert.GetCert(context.Configuration) ); return handler; });All reactions