locked
Membership.GetUser() returns null even though the logged in user is identifed (User.Identitiy.IsAuthenticated returns true) RRS feed

  • Question

  • User-1943149736 posted

    I have been googling for the last few hours with no luck, any help will be greatly appreciated

    Using VS2008, I implemented a windows authentication based membership using Scott's awesome post at http://weblogs.asp.net/scottgu/pages/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server.aspx

     Everything seems to be working fine, users are properly authenticated, role access works fine etc. I cannot, however, receive the user information with GetUser(). This returns null for some reason...

     I am making sure that I logged in by using User.Identity.IsAuthenticated and get true. Also User.Identity.Name returns the user name properly

    I assume my connection string works fine as authentication works well

    <add name="QualityConnectionString" connectionString="Data Source=tssloceng1\sqlexpress;Initial Catalog=Quality;Integrated Security=True;" providerName="System.Data.SqlClient"/>

     ASPNET has all the necessary rights on the DB including db_membership_full

     MembershipUser cUser = Membership.GetUser(); ---> cUser is always null :(

     I am kind of stuck here and am not really sure what else I can try...

     

    Thursday, January 8, 2009 2:44 AM

Answers

  • User-2008565010 posted

    Using VS2008, I implemented a windows authentication based membership using Scott's awesome post at http://weblogs.asp.net/scottgu/pages/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server.aspx

    As per the example ..you are using Windows Authentication and not Forms Authentication. i.e. you are not using membership providers. You are just using Roles Providers.

    Everything seems to be working fine, users are properly authenticated, role access works fine etc. I cannot, however, receive the user information with GetUser(). This returns null for some reason...

     I am making sure that I logged in by using User.Identity.IsAuthenticated and get true. Also User.Identity.Name returns the user name properly

    That is your windows Authenticated User so that all works.

    I assume my connection string works fine as authentication works well

    <add name="QualityConnectionString" connectionString="Data Source=tssloceng1\sqlexpress;Initial Catalog=Quality;Integrated Security=True;" providerName="System.Data.SqlClient"/>

     ASPNET has all the necessary rights on the DB including db_membership_full

     MembershipUser cUser = Membership.GetUser(); ---> cUser is always null :(
     

    Since you are not using Membership Provider --> MembershipUser cUser = Membership.GetUser(); will always return null.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2009 11:11 AM
  • User-2008565010 posted

    I could manually query the database with the UserName to find the ID, however, I would  rather do this through a defined classes if possible. After all it is still in the tables that ASPNET created automatically and it uses it to retrieve the roles etc. (Roles.AddUserToRole())

    Yes ..since you have roles provider enabled it will create an entry in aspnet_Users table and I believe not in Membership table.

    The stored_Proc used by GetUser(username) is aspnet_Membership_GetUserByName which uses Membership table as well to return membership info.

    So one option would be to write your own GetUser function and a storedProc that will return the info you want from aspnetUsers table.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2009 3:00 PM

All replies

  • User-1956254743 posted

    Check this if it helps you : http://forums.asp.net/p/939408/1119989.aspx

    Thursday, January 8, 2009 3:04 AM
  • User-1545654404 posted

     Use GetUser() Method As GetUser(UserName).

    By providing the username to getuser method, u will get all info of that user.

    Thursday, January 8, 2009 5:46 AM
  • User-2008565010 posted

    Using VS2008, I implemented a windows authentication based membership using Scott's awesome post at http://weblogs.asp.net/scottgu/pages/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASP.NET-2.0-using-Windows-Authentication-and-SQL-Server.aspx

    As per the example ..you are using Windows Authentication and not Forms Authentication. i.e. you are not using membership providers. You are just using Roles Providers.

    Everything seems to be working fine, users are properly authenticated, role access works fine etc. I cannot, however, receive the user information with GetUser(). This returns null for some reason...

     I am making sure that I logged in by using User.Identity.IsAuthenticated and get true. Also User.Identity.Name returns the user name properly

    That is your windows Authenticated User so that all works.

    I assume my connection string works fine as authentication works well

    <add name="QualityConnectionString" connectionString="Data Source=tssloceng1\sqlexpress;Initial Catalog=Quality;Integrated Security=True;" providerName="System.Data.SqlClient"/>

     ASPNET has all the necessary rights on the DB including db_membership_full

     MembershipUser cUser = Membership.GetUser(); ---> cUser is always null :(
     

    Since you are not using Membership Provider --> MembershipUser cUser = Membership.GetUser(); will always return null.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2009 11:11 AM
  • User-1943149736 posted

     guru_sarkar, your explanation makes sense, however, how do I access the UserID then? I could manually query the database with the UserName to find the ID, however, I would  rather do this through a defined classes if possible. After all it is still in the tables that ASPNET created automatically and it uses it to retrieve the roles etc. (Roles.AddUserToRole())

     Thanks much...

    Reshma, I tried that already and it doesn't work. It wouldn't make any sense if it worked as GetUser() should already be aware of the current logged in user (apparently in my case it is not as I am using the windows authentication).
    Thursday, January 8, 2009 2:44 PM
  • User-2008565010 posted

    I could manually query the database with the UserName to find the ID, however, I would  rather do this through a defined classes if possible. After all it is still in the tables that ASPNET created automatically and it uses it to retrieve the roles etc. (Roles.AddUserToRole())

    Yes ..since you have roles provider enabled it will create an entry in aspnet_Users table and I believe not in Membership table.

    The stored_Proc used by GetUser(username) is aspnet_Membership_GetUserByName which uses Membership table as well to return membership info.

    So one option would be to write your own GetUser function and a storedProc that will return the info you want from aspnetUsers table.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2009 3:00 PM
  • User495510470 posted

     membership.getuser() get the current user.

    Thursday, January 8, 2009 3:43 PM
  • User-1943149736 posted
    Yes ..since you have roles provider enabled it will create an entry in aspnet_Users table and I believe not in Membership table. The stored_Proc used by GetUser(username) is aspnet_Membership_GetUserByName which uses Membership table as well to return membership info. So one option would be to write your own GetUser function and a storedProc that will return the info you want from aspnetUsers table.

     You are right, Membership table is all empty. I will write my own methods then...

     Thanks again very much for the light...

    Thursday, January 8, 2009 3:45 PM