Accessing membership UserId programmatically?

P

perplexed

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.
 
G

Guest

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
 
P

perplexed

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?
 
P

perplexed

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.
 
G

Guest

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
 
K

Ken Cox [Microsoft MVP]

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>
 
P

perplexed

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.
 
J

Juan T. Llibre

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/
===================================
 
P

perplexed

Is there a simple way to access the new column or will it just come
through in the Membership object?
 
J

Juan T. Llibre

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top