Populating MembershipUserCollection Throws Exception

Discussion in 'ASP .Net Security' started by EagleRed, May 5, 2009.

  1. EagleRed

    EagleRed Guest

    I am writing a custom MembershipProvider. A snippet for the GetAllUsers
    method follows.

    // vw_lion_Apps_Membership is a view containing membership data.
    Table<vw_lion_Apps_Membership> lionMembers =
    _dbContext.GetTable<vw_lion_Apps_Membership>();

    var members = from m in lionMembers
    where (m.ApplicationId == appId)
    select m;

    List<vw_lion_Apps_Membership> memberList =
    members.ToList<vw_lion_Apps_Membership>();
    totalRecords = memberList.Count;
    bool approved = false;
    bool lockedOut = false;
    foreach(vw_lion_Apps_Membership member in memberList)
    {
    if (member.IsApproved != null) approved =
    (bool)member.IsApproved;
    else approved = false;
    if (member.IsLockedOut != null) lockedOut =
    (bool)member.IsLockedOut;
    else approved = false;
    MembershipUser user = new MembershipUser(
    this._name,
    _name,
    member.UserId,
    member.LoweredEmail,
    member.PasswordQuestion,
    member.Comment,
    approved,
    lockedOut,
    (DateTime)member.CreateDate,
    (DateTime)member.LastLoginDate,
    (DateTime)member.LastActivityDate,
    (DateTime)member.LastPasswordChangedDate,
    (DateTime)member.LastLockoutDate
    );
    users.Add(user);
    } // end foreach(vw_lion_Apps_Membership member in memberList)

    The problem arises from the providerName parameter in the MembershipUser
    constructor which is the same for each instance to be added to the
    collection.
    This causes the statement, users.Add(user);, to throw an exception because
    apparently the providerName is a key in the MembershipUserCollection. How
    can I deal with this? (I have tried using the UserName in this and I get
    another exception for an invalid providerName.)

    This is confusing.

    Any input would be greatly appreciated.

    Thanks,
    Eagle
     
    EagleRed, May 5, 2009
    #1
    1. Advertisements

  2. EagleRed

    EagleRed Guest

    I found my problem, my bad. The error is in the second parameter in the
    constructor call, _name. This maps to the UserName property of the
    MembershipUser instance which is the key in the collection. The value passed
    is the name of the provider which is the same, of course, for each pass
    through the loop. The correct constructor call is:

    MembershipUser user = new MembershipUser(
    this._name,
    member.LoweredUserName, // correct parameter value
    member.UserId,
    member.LoweredEmail,
    member.PasswordQuestion,
    member.Comment,
    approved,
    lockedOut,
    (DateTime)member.CreateDate,
    (DateTime)member.LastLoginDate,
    (DateTime)member.LastActivityDate,
    (DateTime)member.LastPasswordChangedDate,
    (DateTime)member.LastLockoutDate
    );

    This now works correctly.

    Remember, "A ship of the lip sinks ships."

    Enjoy,

    Eagle
     
    EagleRed, May 7, 2009
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.