Handling a leftover Principal Identity with WebOrb for .NET
In order to authorize calls to a service, WebOrb for .NET stores a principal object created by the authentication handler. The IPrincipal interface has the IsInRole(roleName) method which is responsible for checking if the user represented by the principal’s identity belongs to a role. System administrators can secure individual methods, classes or namespaces using the WebORB configuration file or the management console. Upon authenticating a user, WebOrb stores the Principal Identity in a session variable and subsequent calls make use of the rolebased authorization.
The Problem
The following scenario demonstrates the problem I was having:
- First, a user successfully authenticates and logs into the application
- The user then closes the application browser tab without logging out.
- The browser had multiple tabs open
- A user reopens the application in a new tab and attempts to log in
- The user supplies invalid credentials
- A WebORBAuthenticationException is thrown, preventing WebOrb from creating a new Principal Identity
Everything should be fine since a new Principal Identity wasn’t created right? Wrong.
The previous principal is still stored in the session and is used for authorizing other service calls. Since the user had previously authenticated, that user’s roles will be used for authorization.
The Solution
At the beginning of my CheckCredential method in my AuthenticationHandler, I simply call:
HttpContext.Current.Session.Clear();
Everything works as expected. Since I don’t have any other session information, the Clear() method fulfills my need. If you have other session information, you may need to loop through the session looking for variables of type System.Security.Principal.GenericIdentity and Weborb.Security.Credentials. Remove both of those variables.
Note: I did have some issues using System.Web.HttpContext.Current in my service library. It was unrecognized. I simply had to add a reference to System.Web and all was ok.
