Description
The AuthenticationManager class manages authentication modules that are responsible for client authentication. [Note: The AuthenticationManager is called by WebRequest instances to provide information that is sent to servers to authenticate the client. The authentication process may consist of requests to an authentication server separate from the resource server, as well as any other activities required to properly authenticate a client.]
The AuthenticationManager queries registered authentication modules by calling the IAuthenticationModule.Authenticate method for each module. The first authentication module that returns an Authorization instance is used to authenticate the request. An authentication module, which can be any object that implements the IAuthenticationModule interface, is registered using the Register method. Authentication modules are called in the order in which they are registered.
Applications typically do not access this type directly; it provides authentication services for the WebRequest type.
Example
using System;
using System.Net;
using System.Collections;
public class AuthenticationManagerSample
{
public class MyModule:IAuthenticationModule
{
public MyModule() {}
public String AuthenticationType
{
get
{
return "TEST";
}
}
public bool CanPreAuthenticate
{
get
{
return false;
}
}
public Authorization PreAuthenticate(WebRequest req,
ICredentials cred)
{
return null;
}
public Authorization Authenticate(String ch,
WebRequest req, ICredentials cred)
{
return null;
}
}
private static void ShowTestModule()
{
IEnumerator rm = AuthenticationManager.RegisteredModules;
while(rm.MoveNext())
{
IAuthenticationModule cm = (IAuthenticationModule) rm.Current;
if (cm.AuthenticationType == "TEST")
{
Console.WriteLine("Module '{0}' is registered:", rm.Current);
Console.WriteLine(" - AuthenticationType = {0}",
cm.AuthenticationType);
Console.WriteLine(" - CanPreAuthenticate = {0}",
cm.CanPreAuthenticate);
return;
}
}
Console.WriteLine("Could not find module for "
+ "AuthenticationType = TEST");
}
public static void Main()
{
IAuthenticationModule mod = new MyModule();
Console.WriteLine();
Console.WriteLine("Registering module...");
AuthenticationManager.Register(mod);
ShowTestModule();
Console.WriteLine();
Console.WriteLine("Un-registering module...");
AuthenticationManager.Unregister("TEST");
ShowTestModule();
}
}
The output is
Registering module...
Module 'AuthenticationManagerSample+MyModule' is registered:
- AuthenticationType = TEST
- CanPreAuthenticate = False
Un-registering module...
Could not find module for AuthenticationType = TEST
|