Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# marquis 7/28/2007 3:13 PM
I need to store extra data for users using our website. To achieve this I extended the Cuyahoga.User and mapped my class using the NHibernate joined-subclass construction.
This almost works perfect, but there's one particularly annoying situation which produces an InvalidCastException. I'll explain:

- I do a login using the standard Cuyahoga user module.
- Go to my own module and try to retrieve the extra user data by Id, using the Cuyahoga user id.

I've tried to do this in 2 ways.
First one: base.NHSession.Load
Second: IList results = crit.List();

In both cases the InvalidCastException is thrown the very first time I try to acces my module.
When the error is thrown I go back the the Cuyahoga user module, log out, log in again and then when I acces my own module everything works just fine.

I've been looking at this for days now but can't seem to find a solution.
Anybody an idea?


# martijnb 7/28/2007 3:39 PM
In the past, I resolved this issue by creating an extra HttpModule that handles the AuthenticateRequest event. In this handler, the default Cuyahoga user object is replaced with the custom user object.

Hopefully, the code below will give you an idea:
HttpApplication app = (HttpApplication)sender;
if (app.Context.User != null 
	&& app.Context.User.Identity.IsAuthenticated
	&& ! (app.Context.User.Identity is MyCustomUser))
{
	
	// Try to swap the Cuyahoga user with MyCustomUser.
	int userId = Int32.Parse(app.Context.User.Identity.Name);

	if (app.Context.User.Identity is Cuyahoga.Core.Domain.User)
	{
		// Evict current Cuyahoga user from the session to prevent
		// issues with loading a subtype when the base type is already
		// in the session cache.
		this._activeNHSession.Evict(app.Context.User.Identity);	
	}

	MyCustomUserRepository ur = new MyCustomUserRepository();
	MyCustomUser myUser = ur.GetMyCustomUserByUserId(userId);
	if (myUser != null)
	{
		myUser.IsAuthenticated = true;
		app.Context.User = new CuyahogaPrincipal(myUser);
	}
	else
	{
		// This appears a 'normal' Cuyahoga user. Attach it again to the session,
		// otherwise lazy loading fails.
		this._activeNHSession.Lock(app.Context.User.Identity, LockMode.None);
	}
}
# marquis 7/29/2007 12:47 AM (edited 7/29/2007 7:32 PM)
Martijn,

Got rid of the cast error now, but after the Session.Evict, only Cuyahoga user data is retrieved from the database. No data from my own user. Looks like the cast works fine now, but the connection to the database for the subclass is somewhere interrupted.
# marquis 10/28/2009 4:39 PM
Ok, the total solution to extend the cuyahoga user class.

I'll attach the code files to the post.

First create a new class which extends the Cuyahoga.Core.Domain.User class.


namespace Mjd.Modules.Users.Domain
{
public class Alphahelpende : User
{
...

}
}


Then create the hibernate mapping file.

See Alphahelpende.hbm.xml.

That's the easy part, then come the cast exception and authentication problems. As Martijn suggested they can be solved by creating an extra HttpModule that handles the authenticaterequest.
The code provided by Martijn also shows you have to create a custom repository class to load your data.

So there are now 3 steps to take:

- Create an extra HttpModule. In my case AH_AuthenticationModule.cs.
- Create a custom repostory to load the data. In my case AH_Repository.cs.
- Update web.config to add the authenticationmodule. See web_config_changes.txt.


Well, that should do the trick. It works for me now in several websites.
# martijnb 10/28/2009 9:21 PM
Thanks a lot for sharing!
# Constructor 10/28/2009 10:48 PM
Yes, thanks from me also...
# Constructor 10/29/2009 11:01 PM (edited 10/29/2009 11:14 PM)
This is a little off topic but forgive me (since it seems you have found the solution to the threads problem :) )

I was experimenting generally with HttpHandlers and HttpModules within Cuyahoga solution.

I have one question for anyone. If I add the following to httphandlers in the web.config
< add verb="*" path="CustomLogin.aspx" type="My.HTTPHandler.Test.Test1Handler, My.HTTPHandler.Test"/>


...then all works as expected. I noticed on a few examples of HttpHandlers that people were using their own extensions like this:
< add verb="*" path="*.new" type="Custom.HTTPHandler.CustomHandler, Custom.HTTPHandler"/>


http://www.domain.com/filename.new

HTTPHandlers only work within my solution if the extension is aspx. I have seen the 'check file exists' reason etc... Is it IIS mappings?

Any idea?

Not an important issue in the scheme of things but I would like to know if anyone can tell... :)
# martijnb 10/30/2009 9:37 AM
You have to add the new extension to IIS and map it to the ASPNET process.
# Constructor 10/30/2009 11:39 AM
martijnbYou have to add the new extension to IIS and map it to the ASPNET process.

Thought so. Thanks.