4

Using VS2013 Update 1 I created a new WebForms project. This project uses the "new" Identity membership provider. I did not want to use that so I reverted back to the Membership provider (For more detail see Webforms HttpContext.Current.User.Identity.IsAuthenticated always true)

I must still have something still wrong as once the user has authenticated when I inspect:

System.Web.HttpContext.Current.User.Identity.Name

I am getting:

"/"

enter image description here

as the result instead of the name the user entered when they logged in via:

Membership.ValidateUser(txtUserName.Text, txtPassword.Text) ie. why am I not getting the value of txtUserName.Text instead of /

I have verified that the SQL being exectuted when

 HttpContext.Current.User.Identity.Name

is being accessed is returning the expected values via the following SQL:

SELECT 
    [Limit1].[C1] AS [C1], 
    [Limit1].[UserName] AS [UserName], 
    [Limit1].[UserId] AS [UserId], 
    [Limit1].[Email] AS [Email], 
    [Limit1].[PasswordQuestion] AS [PasswordQuestion], 
    [Limit1].[Comment] AS [Comment], 
    [Limit1].[IsApproved] AS [IsApproved], 
    [Limit1].[IsLockedOut] AS [IsLockedOut], 
    [Limit1].[CreateDate] AS [CreateDate], 
    [Limit1].[LastLoginDate] AS [LastLoginDate], 
    [Limit1].[LastActivityDate] AS [LastActivityDate], 
    [Limit1].[LastPasswordChangedDate] AS [LastPasswordChangedDate], 
    [Limit1].[LastLockoutDate] AS [LastLockoutDate]
    FROM ( SELECT TOP (1) 
        [Extent1].[UserId] AS [UserId], 
        [Extent1].[UserName] AS [UserName], 
        [Extent1].[LastActivityDate] AS [LastActivityDate], 
        [Extent3].[Email] AS [Email], 
        [Extent3].[PasswordQuestion] AS [PasswordQuestion], 
        [Extent3].[IsApproved] AS [IsApproved], 
        [Extent3].[IsLockedOut] AS [IsLockedOut], 
        [Extent3].[CreateDate] AS [CreateDate], 
        [Extent3].[LastLoginDate] AS [LastLoginDate], 
        [Extent3].[LastPasswordChangedDate] AS [LastPasswordChangedDate], 
        [Extent3].[LastLockoutDate] AS [LastLockoutDate], 
        [Extent3].[Comment] AS [Comment], 
        1 AS [C1]
        FROM   [dbo].[Users] AS [Extent1]
        INNER JOIN [dbo].[Applications] AS [Extent2] ON [Extent1].[ApplicationId] = [Extent2].[ApplicationId]
        INNER JOIN [dbo].[Memberships] AS [Extent3] ON [Extent1].[UserId] = [Extent3].[UserId]
    )  AS [Limit1]

here is my web.config:

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="MyApp2" />
  </providers>
</membership>
<profile defaultProvider="DefaultProfileProvider">
  <providers>
    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="MyApp2" />
  </providers>
</profile>
<roleManager defaultProvider="DefaultRoleProvider" enabled="true">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="MyApp2" />
  </providers>
</roleManager>

so the issue "must be" that I have the Identity membership provider referenced in some way somewhere. Can anyone help me out here as I am really stuck.

I guess a related question is, is there a HOWTO on how to revert a project from Identity to the previous Membership system?

EDIT

After thinking it was all working after changing the applicationName to "MyApp" when I went back to play with the app again I am back to the old "bad" behavior where I get "/" for the user name. So I changed the database to MyApp2 as well as the web.config and still no difference.

I now also include a screenshot of the tables from the database in case this helps with anything. I really need to figure this out so would appreciate any insights. Am also happy to provide other diagnostics as needed. enter image description here

8
  • Have you got the web.config changes done correctly for the membership, profile and role providers?
    – DavidG
    Jun 6, 2014 at 9:18
  • @DavidG I have added my web.config settings for you to have a look at.
    – TheEdge
    Jun 7, 2014 at 5:01
  • Are you running any custom implementations at all? Noticed that your applicationName value is "/". Could you try changing this value to see if it appears as the user name.
    – NickSuperb
    Jun 11, 2014 at 17:42
  • Membership.ValidateUser just returns boolean. What kind of authentication do you use? I'd suggest debug the process of logging in, which would look like if(membership.Validate()) then SetAuthenticationCookie - and there would be someone who stores incorrect name into the cookie. By the way, query looks like ORM-generated, while Default membership provider uses hardcoded one. And where clause is missing...
    – mikalai
    Jun 12, 2014 at 12:39
  • I think it's possible your issue lies in the applicationname field in all three of the web.config excerpts, as that form of Membership (by default) ties itself to an entry in an Application table in the database. I'd try inserting a usable name in the applicationname field instead of "/".
    – devstruck
    Jun 12, 2014 at 21:35

1 Answer 1

3

Well it is always the simple solution isn't it?

As it turns out I had not placed my ASPX code for my login page within an aspx:login. This meant that the cookie was not getting set correctly as all I was doing was Membership.RedirectFromLogin().

So once I put things into an aspx:login all was well. Hope this helps someone else in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.