Populating MembershipUserCollection Throws Exception

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

  1. 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
     
    , May 5, 2009
    #1
    1. Advertising

  2. 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

    "" wrote:

    > 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
     
    , May 7, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xenophon
    Replies:
    1
    Views:
    3,360
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=
    Feb 28, 2006
  2. Jeff
    Replies:
    6
    Views:
    1,721
  3. Bogdan
    Replies:
    0
    Views:
    622
    Bogdan
    Sep 17, 2008
  4. Jeff
    Replies:
    1
    Views:
    1,020
  5. Replies:
    0
    Views:
    228
Loading...

Share This Page