Losing reference when converting from MembershipUs
Hi all.
I hope one of ya guys can cast a light over what is going on here.. When calling GetUser in MembershipProvider I lose the user object in what can best be illustrated with this code snippet:
MembershipUser user = MembershipProvider.GetUser(UserName, true);
Of course it's not as simple but let me elaborate.
I use Firebird as database and hence I use the Fb providers (FbMembershipProvider etc.) but due to design faults or my machine setup I've had to make my own implementations. My own custom providers are really simple.Here are some code snippets that should explain how they works:
// PrimeMembership.cs
// FbMembershipProvider doesn't read from web.config!?
public class PrimeMembership : FbMembershipProvider
{
public void ForceInitialize()
{
if (base.ApplicationName != "PrimeBooking")
{
NameValueCollection config = new NameValueCollection();
config.Add("connectionStringName", "Membership");
config.Add("enablePasswordRetrieval", "false");
...
base.Initialize("FirebirdMembershipProvider", config);
}
}
}
//PrimeBookingUser.cs
public interface IPrimeBookingUser
{
int ArtistID { get; }
UserType Role { get; }
}
/// <summary>
/// Same as <see cref="MembershipUser"/>MembershipUser but with
/// Artist ID and Role
/// </summary>
public sealed class PrimeBookingUser : MembershipUser, IPrimeBookingUser
{
...
/// <summary>
/// Constructor
/// </summary>
public PrimeBookingUser()
{
DataAccessLayerManager dal = DataAccessLayerManager.GetDALManager();
_artistID = (int)dal.OpenQueryScalar("GetArtistID", Parameter.GetParameter("MembershipGUID", this.ProviderUserKey,
System.Data.DbType.Guid, System.Data.ParameterDirection.Input));
}
}
Now that you have seen my custom implementation let's go to the code that causes the trouble. I have made a simple caching class called CacheManager (it's not really a manager but the name will hunt me until I make it a full blown manager) that uses the Cache class from ASP.NET.
1 /// <summary>
2 /// Gets the user.
3 /// </summary>
4 /// <returns>A user class that inherit from <see cref="MembershipUser">MembershipUser</see></returns>
5 public User GetUser() where User : MembershipUser
6 {
7 // First check to see if an instance of this user already exists in the Cache.
8 User cachedContact;
9
10 cachedContact = _context.Cache[_context.User.Identity.Name] as User;
11
12 if (null == cachedContact || cachedContact.ProviderUserKey == null)
13 {
14 if (_context.User.Identity.IsAuthenticated)
15 {
16 PrimeMembership provider = new PrimeMembership();
17 // force membership provider to initialize
18 provider.ForceInitialize();
19 // cachedContact is always null
20 cachedContact = provider.GetUser(_context.User.Identity.Name, true) as User;
21 }
22 else
23 {
24 FormsAuthentication.RedirectToLoginPage("msg=Du har været inaktiv i for lang "
25 + "tid. Log venligst på igen.");
26 }
27 }
28 return cachedContact;
29 }
Line 20 is the trouble maker. I have debugged the source code of FbMembershipProvider (the good thing of open-source) and it returns the user just fine. I don't know why I loose the reference but maybe it has to do with that the constructor of PrimeBookingUser is never called?
Anyway, I'll appreciate any help - I know this is a long post! Regards, Jon.
- mega
Moving to C# .NET
__________________
- mega
Aspiring JavaScript Ninja
|