User.Identity.Name works in single aspx page, doenst work in use with MasterPage

Last post 01-09-2008 9:14 AM by Hao1981. 4 replies.

Sort Posts:

  • User.Identity.Name works in single aspx page, doenst work in use with MasterPage

    04-11-2005, 5:58 AM
    • Loading...
    • Dretrix
    • Joined on 04-04-2005, 8:41 AM
    • Posts 39

    I use a SQL statment to select the userid from a user with user.identity.name. This works fine in a aspx page. I can format the id to a string a display it in a label.

    SqlCommand intgebruikersid = new SqlCommand("select UserId from aspnet_Users where UserName = '" + User.Identity.Name + "'", conn);

    Now, if i want to do exactly the same with another page it wont display the userid in a label. The only difference this other page has, is that it uses a template with a MasterPage file.

    I am using asp.net2.0

    Thankyou,
    Dretrix

  • Re: User.Identity.Name works in single aspx page, doenst work in use with MasterPage

    04-11-2005, 6:33 AM
    • Loading...
    • Fredrik N
    • Joined on 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,333
    • Moderator

    You don't need to run a query to get the Id if you use the Membership feature.

    You can get the Id from the MembershipUser class.

    Use the Membership.GetUser method to get the MembershipUser class for the current logged in user. The property ProviderUserKey will return the User id.

     

    /Fredrik Normén NSQUARED2
    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog, ASP.Net 2.0 etc

    Cornerstone AB
  • Re: User.Identity.Name works in single aspx page, doenst work in use with MasterPage

    03-18-2006, 3:50 AM
    • Loading...
    • tpang13
    • Joined on 03-18-2006, 8:49 AM
    • Posts 9

    you can try use:

    Page.Identity.Name 

    in master page or someother pages

     

  • Re: User.Identity.Name works in single aspx page, doenst work in use with MasterPage

    03-19-2006, 2:19 AM
    • Loading...
    • tpang13
    • Joined on 03-18-2006, 8:49 AM
    • Posts 9
    page.user.identity.name
  • Re: User.Identity.Name works in single aspx page, doenst work in use with MasterPage

    01-09-2008, 9:14 AM
    • Loading...
    • Hao1981
    • Joined on 01-03-2008, 9:30 AM
    • Netherlands
    • Posts 2

    Would you please elaborate on that?

    I'm trying to retrieve the id of a logged-in user through the Login Name control in VWD2005.
    I would like to put the retrieved id in a label on my page so that it can then by inserted together with other product details in my orders table.

    This is the code i've been fiddling with:


    If User.Identity.IsAuthenticated = False Then
                Server.Transfer("login.aspx")
            End If
            
            If User.Identity.IsAuthenticated = True Then
                
                Dim m As MembershipUser
                
                m = Membership.GetUser()
                Response.Write("ID=" + m.ProviderUserKey.ToString())
                
            End If

     Now I'm getting this error: "Object reference not set to an instance of an object."

    I'm new to all this. I just do not know what is means.

    Thanks in advance.
    Freddy
     

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter