3

The top function is returning a membership user while the second one does not. I've passed in the username for a user for whom the top function gets a result but the second just returns null. Any suggestions to get user based on username?

public static MembershipUser getCCUser(Guid UserID)
{
   return ((MembershipProvider)Membership.Providers["CC_MembershipProvider"]).GetUser(UserID, false);
}

public static MembershipUser getCCUser(string userName)
{
   return ((MembershipProvider)Membership.Providers["CC_MembershipProvider"]).GetUser(userName, false);
}
mcfea
  • 1,129
  • 15
  • 22
  • mcfea look at this link for an alternative solution http://stackoverflow.com/questions/1001527/membership-getuserusername-how-to-return-correct-casing – MethodMan Feb 04 '13 at 05:13
  • also keep in mind the following `Membership.GetUser() will only work for an authenticated user. Otherwise, it's going to return null. To verify you're dealing with an authenticated request call "User.Identity.IsAuthenticated" on the page.` – MethodMan Feb 04 '13 at 05:15
  • I think you may misunderstand this particular GetUser call (i.e. with the parameters of userID or Username and the Boolean value of whether they are on line or not)http://msdn.microsoft.com/en-us/library/77f10054(v=vs.100).aspx. It doesn't need to have an authenticated user. You are correct about GetUser() though. Interesting idea the poster has about case of the username. I'll give that a whirl. – mcfea Feb 04 '13 at 05:27
  • I will post a link where you can read more about what I have found in reference to my comment. http://stackoverflow.com/questions/6329181/get-current-membershipuser-in-forms-authentication – MethodMan Feb 04 '13 at 05:29
  • No luck on case... Thanks for the suggestion tho. – mcfea Feb 04 '13 at 05:33

3 Answers3

6

The applicationNames in the web.config should match the application name in aspnet_Applications table

piris
  • 1,547
  • 3
  • 22
  • 26
3

I found out what was really causing the issue. Recently I had changed the application name so as not to conflict with other applications in membership. I had forgotten to update the application name in the membership section of my web.config file:

Web.config (snipped for brevity):

<membership>
      <providers>
        <clear/>
        <add name="AA_MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="AA_MEMBERSHIP" applicationName="/"/>
        <add name="CC_MembershipProvider" applicationName="/"/>
      </providers>
</membership>

Should have read:

<membership>
      <providers>
        <clear/>
        <add name="AA_MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="AA_MEMBERSHIP" applicationName="/"/>
        <add name="CC_MembershipProvider" applicationName="CCPortal"/>
      </providers>
</membership>

The interesting takeaway here is that I found the user when using their Guid based user ID, but that membership needs to have the right application scope when looking for the user based on the string username.

mcfea
  • 1,129
  • 15
  • 22
0

Ok. So this is only one possible way but basically I just did the look up using Entity Framework. I'm still open to suggestions.

public static MembershipProvider CCMembershipProvider
{
    get
    {
        return ((MembershipProvider)Membership.Providers["CC_MembershipProvider"]);
    }
}

public static MembershipUser getCCUser(Guid UserID)
{
    return CCMembershipProvider.GetUser(UserID, false);
}

public static MembershipUser getCCUser(string userName)
{
    //This function is BS.  For some reason userName always gets null.  Unfortunately UsersInRole only gives a string[]
    //Soo... Here is the jenky workaround...
    userName = userName.ToLower();

    CCPortal.MEMBERSHIPEntities context = new CC.MEMBERSHIPEntities();

    CCPortal.aspnet_Users user = context.aspnet_Users.SingleOrDefault(u => u.LoweredUserName == userName);

    return getCCUser(user.UserId);
    //This is what We should be using....
    //return CCMembershipProvider.GetUser(userName, false);
}
mcfea
  • 1,129
  • 15
  • 22