Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 26th, 2007, 12:09 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default 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);
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;
10 cachedContact = _context.Cache[_context.User.Identity.Name] as User;
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop Down List losing value greenspacechunks ASP.NET 2.0 Basics 5 November 29th, 2007 09:44 PM
Losing it with Labels.. Doh! rit01 ASP.NET 2.0 Basics 5 March 15th, 2006 04:40 PM
Losing variables bluemat XSLT 2 October 16th, 2004 01:59 AM
Losing style property stephanel VS.NET 2002/2003 10 August 12th, 2004 07:50 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.