C# “如何”;WebRequest";对象确定;IAAuthenticationModule“;要使用的实现?
我想使用System.Net的类来定义一个应用程序的基本或承载授权头 AuthenticationManager提供了一种注册方法来添加新模块(的实现)。这表明可以注册多个模块,并且有一种方法可以选择其中一个模块 我假设模块选择必须通过给出在模块的“AuthenticationType”属性中定义的值来完成。我在传递给我的“WebRequest”的文件中定义它 我尝试创建并保存两个模块:C# “如何”;WebRequest";对象确定;IAAuthenticationModule“;要使用的实现?,c#,authentication,authorization,basic-authentication,bearer-token,C#,Authentication,Authorization,Basic Authentication,Bearer Token,我想使用System.Net的类来定义一个应用程序的基本或承载授权头 AuthenticationManager提供了一种注册方法来添加新模块(的实现)。这表明可以注册多个模块,并且有一种方法可以选择其中一个模块 我假设模块选择必须通过给出在模块的“AuthenticationType”属性中定义的值来完成。我在传递给我的“WebRequest”的文件中定义它 我尝试创建并保存两个模块: 用于重新定义基本授权(禁用预验证)的模块(我使用Microsoft文档的示例:) 用于授权承载的模块 然
- 用于重新定义基本授权(禁用预验证)的模块(我使用Microsoft文档的示例:)
- 用于授权承载的模块
//我删除了以前的基本模块
AuthenticationManager.Unregister(“基本”);
//我注册了两个模块
AuthenticationManager.Register(customBasicModule);
AuthenticationManager.Register(customBearerModule);
但这似乎总是第一个被调用的记录模块
我的测试代码:
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(“http://example.com");
var cache=new-CredentialCache();
添加(新Uri(“http://example.com新的网络凭证(“用户”、“密码”);
request.Method=“GET”;
request.Credentials=cache;
HttpWebResponse=(HttpWebResponse)request.GetResponse();
我希望调用“customBasicModule”,因为我在该模块的属性“AuthenticationType”中以及在“CredentialCache”中指示了“Basic”。
但是如果我先注册“customBearerModule”,它将被调用
模块:
公共类基本身份验证模块:IAAuthenticationModule
{
private const string BASIC_SCHEME=“BASIC”;
公共bool CanPreAuthenticate=>false;
公共字符串AuthenticationType=>基本\u方案;
公共授权身份验证(字符串质询、WebRequest请求、ICredentials凭据)
{
//从ICredentials获取Basic的一些代码
}
公共授权预验证(WebRequest请求、ICredentials凭据)
{
返回null;
}
}
公共类BeareAuthenticationModule:IAAuthenticationModule
{
private const string BEARER_SCHEME=“BEARER”;
公共bool CanPreAuthenticate=>false;
公共字符串AuthenticationType=>BEARER\u方案;
公共授权身份验证(字符串质询、WebRequest请求、ICredentials凭据)
{
//从ICredentials获取承载的一些代码
}
公共授权预验证(WebRequest请求、ICredentials凭据)
{
返回null;
}
}
AuthenticationManager将始终按照注册的顺序调用所有IAAuthenticationModule,直到其中一个返回非空授权实例
其思想是,每个IAuthenticationModule实现都需要根据它们能够执行的操作验证质询参数,如果它们不匹配,则返回null
因此,您的实现应该
public class BasicAuthenticationModule : IAuthenticationModule
{
private const string BASIC_SCHEME = "Basic";
public bool CanPreAuthenticate => false;
public string AuthenticationType => BASIC_SCHEME;
public Authorization Authenticate(string challenge, WebRequest request, ICredentials credentials)
{
if (!challenge.StartWith(BASIC_SCHEME)) return null;
// Some code to get Basic from ICredentials
}
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
{
return null;
}
}
public class BearerAuthenticationModule : IAuthenticationModule
{
private const string BEARER_SCHEME = "Bearer";
public bool CanPreAuthenticate => false;
public string AuthenticationType => BEARER_SCHEME;
public Authorization Authenticate(string challenge, WebRequest request, ICredentials credentials)
{
if (!challenge.StartWith(BEARER_SCHEME)) return null;
// Some code to get Bearer from ICredentials
}
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
{
return null;
}
}
还请注意,挑战是服务器在第一次未经授权的响应(401)后发送的WWW Authenticate标头的内容,该响应与您在CredentialCache中写入的“基本”无关。谢谢您的回答