The Authentication Service
Context
A typical Enterprise Application requires a user to login first before using the application. DevForce currently does not provide a central security context out-of-the-box. Login on the client is handled by the EntityManager and on the server by a LoginManager. DevForce provides means to propagate the security context from one EntityManager to another, but it requires some code to be implemented as part of the application.
The DevForce Application Framework provides an authentication service that implements all these pieces and provides the required functionality for most types of applications. If desired, the authentication service can be sub-classed and extended. The purpose of the authentication service is to provide a central service to handle the login, logout and current user information. The service is made available through the MEF container, so that it can be injected into any class that requires the current user or needs to login/logout. Furthermore, the DevForce Application Framework itself looks for the authentication service to make sure that any EntityManager created by the application, either through an EntityManagerProvider or through some other means, automatically inherits the current security context.
The authentication service works in conjunction with a DevForce LoginManager.
See also:
- The Login Process: http://drc.ideablade.com/xwiki/bin/view/Documentation/authentication-details
- Use ASP.NET Security: http://drc.ideablade.com/xwiki/bin/view/Documentation/security-aspnet
Enable the Authentication Service
The authentication service consist of two parts. The IAuthenticationService interface and the AuthenticationService<T> implementation. To enable the service, it simply needs to be made available to the application. This is typically accomplished in the application’s bootstrapping logic.
public class AppBootstrapper : FrameworkBootstrapper<MainFrameViewModel> { protected override void InitializeCompositionBatch(CompositionBatch batch) { base.InitializeCompositionBatch(batch); // Inject the authentication service into the framework. batch.AddExportedValue<IAuthenticationService>(new AuthenticationService<NorthwindIBEntities>()); } }
The AuthenticationService<T> implementation is a generic implementation that works with the concrete type of your application’s EntityManager. This can be the generic EntityManager type or any subclass thereof. The only requirement is that an EntityManagerProvider for the same type of EntityManager can be located via MEF.
Using the Authentication Service
Once the authentication service is enabled it can be used by simply injecting it everywhere it is needed:
[Export] public class LoginViewModel { private readonly IAuthenticationService _authenticationService; [ImportingConstructor] public LoginViewModel(IAuthenticationService authenticationService) { _authenticationService = authenticationService; } // Details omitted for clarity public void Login() { _authenticationService .LoginAsync(new LoginCredential(_username, _password, null), () => { // Do something if login successful. }, e => { // Display error message if login failed. }); } }