Accessing membership UserId programmatically?

Discussion in 'ASP .Net' started by perplexed, Jan 27, 2007.

  1. perplexed

    perplexed Guest

    After a user logs in (ASP.NET 2.0 membership/roles), how do I
    programmatically access their UserId and other information stored in
    the ASP.NET database structure? A user may login at Login.aspx and
    then they are redirected to somepage.aspx. somepage.aspx doesn't have
    a reference to the login object and can't access UserId.

    Also, are there any issues with changing the UserId column from a
    uniqueidentifier to an integer IDENT?

    Thanks.
     
    perplexed, Jan 27, 2007
    #1
    1. Advertising

  2. All of the membership services can be accessed programmatically via
    System.Web.Security.Membership class.

    I would not recommend changing the database schema that MS uses for
    membership info. My gut feeling is that would be bad.

    Jason Vermillion

    "perplexed" wrote:

    > After a user logs in (ASP.NET 2.0 membership/roles), how do I
    > programmatically access their UserId and other information stored in
    > the ASP.NET database structure? A user may login at Login.aspx and
    > then they are redirected to somepage.aspx. somepage.aspx doesn't have
    > a reference to the login object and can't access UserId.
    >
    > Also, are there any issues with changing the UserId column from a
    > uniqueidentifier to an integer IDENT?
    >
    > Thanks.
    >
    >
     
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=, Jan 28, 2007
    #2
    1. Advertising

  3. perplexed

    perplexed Guest

    Thanks. I'd like to display a more friendly userid. Do you know of a
    way I can generate one without disturbing the membership/roles
    database schema?
     
    perplexed, Jan 28, 2007
    #3
  4. Add a column to the database and display that.





    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "perplexed" <> wrote in message
    news:...
    > Thanks. I'd like to display a more friendly userid. Do you know of a
    > way I can generate one without disturbing the membership/roles
    > database schema?
    >
     
    Juan T. Llibre, Jan 28, 2007
    #4
  5. perplexed

    perplexed Guest

    On Jan 27, 6:22 pm, "Juan T. Llibre" <>
    wrote:
    > Add a column to the database and display that.


    Juan, that's right but I guess I was wondering more about
    consequences. It's probably a shot in the dark. It's only testing
    right now so no big deal if something breaks.
     
    perplexed, Jan 28, 2007
    #5
  6. You would probably be ok. I'd be a bit reluctant to do this, myself, though.

    The problems might be if MS ever makes a change to the schema or stored
    procs that would blow up any of your mods. Also, you need to remember to
    make the changes in the membership schema anytime you switch the datastore
    that holds your userids (say when you migrate from dev to QA, and then to
    prodution, or if you ever had to rebuild your production ASPNETDB).

    You might check to see about using personalization properites via the web
    config. I think you can use this to create custom name/value pairs that are
    stored in ASPNETDB.aspnet_Profile. See this
    http://www.ondotnet.com/pub/a/dotnet/2004/08/16/whidbey_personalization.html?page=1

    Jason Vermillion


    "perplexed" wrote:

    >
    >
    > On Jan 27, 6:22 pm, "Juan T. Llibre" <>
    > wrote:
    > > Add a column to the database and display that.

    >
    > Juan, that's right but I guess I was wondering more about
    > consequences. It's probably a shot in the dark. It's only testing
    > right now so no big deal if something breaks.
    >
    >
     
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=, Jan 28, 2007
    #6
  7. There are a number of stored procedures for fetching data about users. For
    example, try:

    sqldsrc.SelectCommand = "SELECT Email FROM " & _
    "vw_aspnet_MembershipUsers WHERE (UserName = 'kencox')"

    Is that what you meant?

    Ken
    Microsoft MVP [ASP.NET]

    <%@ Page Language="VB" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <script runat="server">
    Protected Sub Page_Load _
    (ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If Not IsPostBack Then
    ' By Ken Cox [Microsoft MVP]
    ' August 24, 2006
    ' Please improve on this hack! --kc
    ' Declare the dataview, sqldatasource
    ' and connection
    Dim dv As New Data.DataView
    Dim sqldsrc As New SqlDataSource
    Dim connectionStrings As _
    ConnectionStringSettingsCollection = _
    System.Web.Configuration. _
    WebConfigurationManager.ConnectionStrings
    ' Set the select command to use the built-in view
    ' and narrow the view down to the username
    sqldsrc.SelectCommand = "SELECT Email FROM " & _
    "vw_aspnet_MembershipUsers WHERE (UserName = 'kencox')"
    ' Make sure we are treating it as a text
    ' query rather than as a stored procedure
    sqldsrc.SelectCommandType = SqlDataSourceCommandType.Text
    ' Tell the sqldatasource to return a dataset or datatable
    sqldsrc.DataSourceMode = SqlDataSourceMode.DataSet
    ' Set the ID for good form
    sqldsrc.ID = "sqldatasource1"
    ' Fetch the connection string from the web.config
    sqldsrc.ConnectionString = connectionStrings.Item _
    ("ASPNETDBConnectionString").ConnectionString
    ' Get the retrieved data into the dataview by
    ' calling the Select method with no arguments
    dv = sqldsrc.Select(DataSourceSelectArguments.Empty)
    ' Put the retrieved value from the "email" column
    ' into the label
    Label1.Text = dv.Table.DefaultView(0).Item("email")
    End If
    End Sub
    </script>


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Get the email address of a Membership row</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <div>
    <p>
    &nbsp;<asp:label id="Label1"
    runat="server"></asp:label></p>
    &nbsp;</div>
    </div>
    </form>
    </body>
    </html>



    "perplexed" <> wrote in message
    news:...
    > After a user logs in (ASP.NET 2.0 membership/roles), how do I
    > programmatically access their UserId and other information stored in
    > the ASP.NET database structure? A user may login at Login.aspx and
    > then they are redirected to somepage.aspx. somepage.aspx doesn't have
    > a reference to the login object and can't access UserId.
    >
    > Also, are there any issues with changing the UserId column from a
    > uniqueidentifier to an integer IDENT?
    >
    > Thanks.
    >
     
    Ken Cox [Microsoft MVP], Jan 28, 2007
    #7
  8. perplexed

    perplexed Guest

    On Jan 27, 11:08 pm, "Ken Cox [Microsoft MVP]"
    <> wrote:
    > There are a number of stored procedures for fetching data about users. For
    > example, try:
    >
    > sqldsrc.SelectCommand = "SELECT Email FROM " & _
    > "vw_aspnet_MembershipUsers WHERE (UserName = 'kencox')"
    >
    > Is that what you meant?


    I meant existing structures rather than coding it all by hand. I few
    sprocs will do it though.
     
    perplexed, Jan 28, 2007
    #8
  9. re:
    > Juan, that's right but I guess I was wondering more about consequences.


    None, whatsoever.

    The caveat you got earlier was about changing the schema for existing fields,
    not towards adding more fields. That's perfectly doable without consequences.

    In fact, you can replace the whole database *and* membership provider with custom ones.

    See :
    http://www.devx.com/asp/Article/29256
    and
    http://weblogs.asp.net/scottgu/arch...entication_2C00_-and-Security-Resources-.aspx


    The complete source code to the Membership provider, and all providers, is at:
    http://weblogs.asp.net/scottgu/arch...2.0-Providers-Now-Available-for-Download.aspx

    Modify it to taste...

    Finally, everything you could possibly want to know
    about Providers is found in Jeff Prosise's MSDN article :

    http://msdn2.microsoft.com/en-us/asp.net/aa336558.aspx






    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "perplexed" <> wrote in message
    news:...
    >
    >
    > On Jan 27, 6:22 pm, "Juan T. Llibre" <>
    > wrote:
    >> Add a column to the database and display that.

    >
    > Juan, that's right but I guess I was wondering more about
    > consequences. It's probably a shot in the dark. It's only testing
    > right now so no big deal if something breaks.
    >
     
    Juan T. Llibre, Jan 28, 2007
    #9
  10. perplexed

    perplexed Guest

    Is there a simple way to access the new column or will it just come
    through in the Membership object?
     
    perplexed, Jan 28, 2007
    #10
  11. re:
    > Is there a simple way to access the new column


    That depends on what you call simple.

    re:
    > will it just come through in the Membership object?


    You'll have to modify the membership provider class.

    Get the details for that class here:
    http://msdn2.microsoft.com/en-us/library/aa478949.aspx




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "perplexed" <> wrote in message
    news:...
    > Is there a simple way to access the new column or will it just come
    > through in the Membership object?
     
    Juan T. Llibre, Jan 28, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    722
  2. Replies:
    3
    Views:
    498
  3. Replies:
    1
    Views:
    666
  4. mazdotnet
    Replies:
    0
    Views:
    669
    mazdotnet
    Sep 12, 2007
  5. TdarTdar
    Replies:
    15
    Views:
    276
    André da Silva Carrilho
    Jul 28, 2006
Loading...

Share This Page