简体   繁体   English

如何在 Flurl 库中使用 Windows 身份验证?

[英]How do I use Windows Authentication with the Flurl library?

Flurl has methods for doing OAuth and Basic authentication: Flurl具有执行 OAuth 和基本身份验证的方法:

await url.WithBasicAuth("username", "password").GetJsonAsync();
await url.WithOAuthBearerToken("mytoken").GetJsonAsync();

but how do I do Windows authentication using the currently logged in user?但是如何使用当前登录的用户进行 Windows 身份验证? The HttpClientHandler that Flurl is built on top of has a property UseDefaultCredentials but I don't know how to utilize that within Flurl. Flurl 建立在其上的 HttpClientHandler 有一个属性 UseDefaultCredentials 但我不知道如何在 Flurl 中使用它。

var httpClient = new HttpClient(new HttpClientHandler() 
{
    UseDefaultCredentials = true
});

Flurl intelligently reuses the HttpClientHandler for each domain, so you don't want to set the UseDefaultCredentials each time it runs. Flurl智能地为每个域重用HttpClientHandler,因此您不希望每次运行时都设置UseDefaultCredentials。 Instead, you can modify the HttpClientFactory to return one that's configured to UseDefaultCredentials. 相反,您可以修改HttpClientFactory以返回配置为UseDefaultCredentials的HttpClientFactory。

public class UseDefaultCredentialsClientFactory : DefaultHttpClientFactory
{
    public override HttpMessageHandler CreateMessageHandler()
    {
        return new HttpClientHandler { UseDefaultCredentials = true };
    }
} 

Then you need to tell Flurl to use this factory for the domains you want to use Windows authentication for. 然后,您需要告诉Flurl将此工厂用于您要使用Windows身份验证的域。

public static class FlurlConfiguration
{
    public static void ConfigureDomainForDefaultCredentials(string url)
    {
        FlurlHttp.ConfigureClient(url, cli =>
            cli.Settings.HttpClientFactory = new UseDefaultCredentialsClientFactory());
    }
}

Then you simply need to call this once on startup for each domain. 然后,您只需在启动时为每个域调用一次。 For ASP.NET, the Application_Start method in your global application class is a good place for it. 对于ASP.NET,全局应用程序类中的Application_Start方法是一个很好的选择。

FlurlConfiguration.ConfigureDomainForDefaultCredentials("https://example.com");
FlurlConfiguration.ConfigureDomainForDefaultCredentials("http://services.example.com");

Credit goes to Todd Menier for explaining this to me . 感谢Todd Menier向我解释这一点

If it is still relevant.如果它仍然相关。 You can set credentials, something like this你可以设置凭据,像这样

 ((HttpClientHandler)url.Client.HttpMessageHandler).Credentials = new NetworkCredential(userName, password);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flurl 中为 Web 请求使用代理? - How can I use proxies for web requests in Flurl? 如何在 HttpClient 实例中使用 Flurl - How to use Flurl with instance of HttpClient 如何使用 FlUrl 进行 XML POST - How to do XML POST with FlUrl 问 - 如何在“PostJsonAsync”Flurl 中使用“参数”这个词 - Ask - How to use the word 'params' in 'PostJsonAsync' Flurl 如何在Flurl.Http上使用Polly? - How to use Polly with Flurl.Http? 如何创建用于身份验证的 Windows 服务,自定义 Web 应用程序可使用该服务对 AD 用户进行身份验证? - How do I create a windows service for authentication that custom web applications can use to authenticate AD users? 如何在 Flurl / Json.net 中支持对 JSON 有效负载的向后中断更改? - How do I support a backward breaking change to JSON payload in Flurl / Json.net? 如何在 Microsoft.Rest.ServiceClient 中使用 Windows 身份验证 - How can I use Windows Authentication with Microsoft.Rest.ServiceClient 在FlUrl中我尝试使用EnableCookies()并抛出一个空引用异常 - in FlUrl I try to use EnableCookies() and it throw a null reference exception 如何将 web 请求中的 windows 身份验证令牌传递给 api? - How do I pass the windows authentication token on a web request to an api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM