Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2006, 03:04 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing Membership Data Programatically

I am attempting to access (in code) the UserID created in the ASPNETDB database. I have been able to get the UserID if I specify the username (using the MembershipUserCollection and GetAllUsers()). However I am interested in getting the UserID for the current user. The text (Professional ASP.NET 2.0) and MS Help suggest using Membership.GetUser(). This generates errors. Has anyone successfully been able to access the UserID of the current (logged-in) user?
Thanks.

Fred K. Augustine, Jr
 
Old February 10th, 2006, 03:35 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Fred,

Membership.GetUser() should return the user. But it only returns a user when you're logged in successfully. Are you using Forms authentication?

Once you have the user, you can get its ProviderUserKey which is the unique ID.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 10th, 2006, 03:50 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,
Thanks for the reply. I am using forms authentication and I have tried to use Membership.GetUser() and ProviderUserKey. Here's my code:

Dim memUser As MembershipUser
Dim memName As MembershipUserCollection
memName = Membership.GetAllUsers()
memUser = Membership.GetUser()
Dim uid As Guid = memUser.ProviderUserKey
Label1.Text = uid.ToString

This code will work if I specify the UserName (ex: memName.Item("matrix") where matrix is a username).




Quote:
quote:Originally posted by Imar
 Hi Fred,

Membership.GetUser() should return the user. But it only returns a user when you're logged in successfully. Are you using Forms authentication?

Once you have the user, you can get its ProviderUserKey which is the unique ID.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Fred K. Augustine, Jr
 
Old February 10th, 2006, 04:24 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you use memName.Item("matrix") you're accessing one of the Members in the collection.

However, using only this:

Dim memUser As MembershipUser = Membership.GetUser()
Dim uid As Guid = memUser.ProviderUserKey
Label1.Text = uid.ToString

should work, provided *you* are logged in using Forms authentication, not Window authentication.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 10th, 2006, 06:28 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,
Thanks again. It works as promised. It appears that my problem was that the login control is in a content page connected to a master and the code will only execute if I place it in a button and click. Placing the code in either one of the Page events or login control events produces the error (object reference must reference an instance of an object). A little further experimentation shows the only way to produce a postback in the content page is via a button click (clicking on the button in the login control does not (apparently) cause a postback event for the content page. Any ideas about this?



Quote:
quote:Originally posted by Imar
 If you use memName.Item("matrix") you're accessing one of the Members in the collection.

However, using only this:

Dim memUser As MembershipUser = Membership.GetUser()
Dim uid As Guid = memUser.ProviderUserKey
Label1.Text = uid.ToString

should work, provided *you* are logged in using Forms authentication, not Window authentication.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Fred K. Augustine, Jr
 
Old February 10th, 2006, 07:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Are you trying to do this in in the Login page?

The Login button does trigger a post back, or otherwise the user could never be authenticated.

However, maybe you're doing this in the wrong event. If you do this in Page_Load, the user is not authenticated yet... Instead, do it in the LoggedIn event of the Login control:
Code:
Protected Sub Login1_LoggedIn(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Login1.LoggedIn
  ' At this point, the code I posted earlier should work
End Sub
HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 15th, 2006, 04:02 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,
Sorry for the delay in responding ...
I've tried just about every event associated with the login control and can't seem to get any reaction to a control event or page event. For example I place a label control in the content page containing the login control and change the text of that label to "YES" in the loggedIn event. When I run the page and log in with a valid user, I get no change to the label. As I said I've attempted this in almost every page event and login control event. However, If I place a button on the content page and place the same code in the button_clic event, the code will execute properly. This doesn't just apply to the membership code, but code as simple as setting the text property of a label or executing a response.write()!! I'm perplexed. Do we have a bug here?

Quote:
quote:Originally posted by Imar
 Are you trying to do this in in the Login page?

The Login button does trigger a post back, or otherwise the user could never be authenticated.

However, maybe you're doing this in the wrong event. If you do this in Page_Load, the user is not authenticated yet... Instead, do it in the LoggedIn event of the Login control:
Code:
Protected Sub Login1_LoggedIn(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Login1.LoggedIn
  ' At this point, the code I posted earlier should work
End Sub
HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Fred K. Augustine, Jr
 
Old February 15th, 2006, 04:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Fred,

Can you show us the code for the page? Both the markup and the Code Behind?

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: New Morning by Nick Cave and the Bad Seeds (Track 10 from the album: Tender Prey) What's This?
 
Old February 15th, 2006, 04:55 PM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[u]Here's the code behind:</u>
Partial Class content
    Inherits System.Web.UI.Page
    Protected uid As Guid

Protected Sub logContent_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles logContent.LoggedIn

        Dim StrLink As String
        Dim memUser As MembershipUser = Membership.GetUser()
        uid = memUser.ProviderUserKey
        StrLink = "details.aspx?uid=" & uid.ToString
        Server.Transfer(StrLink)
    End Sub
Protected Sub lnkProceed_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkProceed.Click
        Dim memUser As MembershipUser = Membership.GetUser()
        uid = memUser.ProviderUserKey
        Label1.Text = uid.ToString
   End Sub
End Class

[u]Here's the HTML Markup</u>
<%@ Page Language="VB" MasterPageFile="~/sgf.master" AutoEventWireup="false" CodeFile="content.aspx.vb" Inherits="content" title="Sample Content Page" %>
<asp:Content ID="cphLog" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    &nbsp;<br />
    <asp:Login ID="logContent" runat="server" BackColor="#FFFBD6" BorderColor="#FFDFAD" BorderPadding="4"
        BorderStyle="Solid" BorderWidth="1px" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="DarkOrange"
        TextLayout="TextOnTop" style="z-index: 100; left: 10px; position: relative; top: -9px">
        <TitleTextStyle BackColor="#FFC080" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
        <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
        <TextBoxStyle Font-Size="0.8em" />
        <LoginButtonStyle BackColor="White" BorderColor="DarkOrange" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Palatino Linotype" Font-Size="0.8em" ForeColor="DarkOrange" />
        <LabelStyle BorderColor="DarkOrange" Font-Names="Palatino Linotype" />
    </asp:Login>
    &nbsp;&nbsp;<br />
    <br />
    <br />
    <asp:LinkButton ID="lnkProceed" runat="server" Style="z-index: 102; left: 345px;
        position: absolute; top: 437px" Width="290px" Visible="True">Click to Proceed</asp:LinkButton>
    <asp:Label ID="Label1" runat="server" Style="left: 77px; position: relative; top: 26px"
        Text="Label" Width="282px"></asp:Label>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
</asp:Content>

The code in the LoggedIn event crashes on [u]uid = memUser.ProviderUserKey.</u> If I comment out the LoggedIn code, I can click on the link button after logging in and display the UserID.

Quote:
quote:Originally posted by Imar
 Hi Fred,

Can you show us the code for the page? Both the markup and the Code Behind?

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: New Morning by Nick Cave and the Bad Seeds (Track 10 from the album: Tender Prey) What's This?
Fred K. Augustine, Jr
 
Old February 15th, 2006, 05:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right I see now. I should have spotted this before.

In the page with the Login control, a call to GetUser doesn't return the user yet. I think this has to do with how authentication works: early in the ASP.NET pipeline, a user is authenticated on each request. Since the page with the login control is already beyond that point, the current request (the post back) isn't authenticated yet, so there is no current MembershipUser associated with the request.

You'll find that if you redirect to the next page your code works fine because the user is then authenticated.

Two ways to fix this:

a) redirect to an immediate page; in the next page, Membership.GetUser() will give you the MembershipUser object for the current request.

b) A much better solution: use one of the overloads of GetUser() and explicitly create a MembershipUser object by specifying the user's name:
Code:
Protected Sub Login1_LoggedIn(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles Login1.LoggedIn
  Dim StrLink As String
  Dim memUser As MembershipUser = Membership.GetUser(Login1.UserName)
  uid = CType(memUser.ProviderUserKey, Guid)
  Server.Transfer(StrLink)
  StrLink = "details.aspx?uid=" & uid.ToString
End Sub
Notice how I am passing Login1.UserName to the GetUser method. The Login control will, obviously, hold the name of the user that just logged in...

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Money Greedy by Tricky (Track 1 from the album: Angels With Dirty Faces) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Membership Profile Data vickyc ASP.NET 2.0 Professional 0 October 11th, 2007 04:12 PM
Accessing Programatically xlr8 BOOK: Beginning C# 2005 Databases 1 April 30th, 2007 08:56 AM
Accessing browser (IE) cache programatically baburman General .NET 7 October 24th, 2006 09:50 AM
Migrate user data to new membership model relaytest49 ASP.NET 2.0 Professional 2 May 24th, 2006 09:44 AM
Using data programatically jasongorringe ASP.NET 2.0 Basics 1 February 10th, 2006 02:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.