 |
| Visual Web Developer 2008 Discuss creating ASP.NET 3.5 sites with Microsoft's Visual Web Developer 2008. If your question is more specific to a piece of code than the Visual tool, see the ASP.NET 3.5 forums instead. If your question is specific to the "Express Edition" be sure to state that in your post. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Web Developer 2008 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
|
|
|
|

January 25th, 2009, 10:20 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Displaying data based on user logged in
First of all - I hope I have posted this in the correct place - if not; please bear with me as I am new to using forums. Thanks.
I am also relatively new to ASP.NET and am experiencing a steep learning curve as I work on a current assignment.
The biggest problem I am having at the moment is with user profiles / log-ins. I am using Visual Web Developer 2008 Express and have been able to get a basic login function to work using the wizards provided in the software. So I can create a new user and then log in as this user. I also have it displaying the name of the logged in user on the page when login has been successful.
I know this information is being stored in the ASPNETDB.mdf, which Visual Web Developer creates for you.
I have my own sql database which will store personal information, (address etc) and rather than try to merge the two I eventually want to link ASPNETDB.mdf with my own, based possibly on the unique user code it provides. However this is not what I am having issue with at the moment (although if you have advice on best practice I am keen to hear it!)
I want to begin to expand the site to show only information relevant to the logged in user - for example initially to use information only from ASPNETDB.mdf, so to display the user code for the logged in user.
To try and achieve this I have followed through creating a datagrid and linking it to a datasource. I realise I must somehow need to specify that the data displayed is only that of the logged in user. I follow through configuring a connection string, and including a Where clause, to try and pick up the logged in user, but this is where it falls apart on me! I don't know where the source I am picking the user name up from needs to be - for example from a session, cookie, form, profile.
I can get it to work in the test; (when I use the 'profile' value as 'username') but this is because I am typing in the user name - I need it to recognise the name of the logged in user as opposed to have them type it each time.
I realise this is a bit garbled but if you can understand what I am trying to do and could offer any help / suggestions, that would be most appreciated. I feel once I get past this hurdle I will be able to make substancial progress, but I have been stumped at this for a while.
|
|

January 25th, 2009, 10:35 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
If you're using the built-in ASP.NET Security mechanism, then the following code gives you the user name of the currently logged on user:
string userName = Membership.GetUser().UserName;
Alternatively, you can get the user's unique ID:
Guid userId = (Guid) Membership.GetUser().ProviderUserKey;
It's in C#, but the same principle holds for a VB site of course....
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

January 25th, 2009, 01:02 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Quote:
Originally Posted by Spider
I don't know where the source I am picking the user name up from needs to be - for example from a session, cookie, form, profile.
I can get it to work in the test; (when I use the 'profile' value as 'username') but this is because I am typing in the user name - I need it to recognise the name of the logged in user as opposed to have them type it each time.
|
Keep in mind that the current user identity is always stored in the current HttpContext. Therefore, from a page, you can always use this:
string userName = Context.User.Identity.Name;
If the current user is not logged in, this value will be an empty string.
I think this is a little easier than Imar's suggestion, as it doesn't require you to use the static methods of Membership, or create a MembershipUser. It also works if you are using Windows authentication instead of the ASP.NET Forms authentication database stuff.
|
|

January 26th, 2009, 06:25 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
|
|
Wheres the sql command?
I am having the same problem and I am able to display the username, my problem is how do I query against this value? I seem to have tried everything (UserName, ID, LoginName, etc) against session, cookie, form.......Nothing. Any help on why I cant capture this?
|
|

January 26th, 2009, 10:02 AM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Quote:
Originally Posted by mashype
I am having the same problem and I am able to display the username, my problem is how do I query against this value? I seem to have tried everything (UserName, ID, LoginName, etc) against session, cookie, form.......Nothing. Any help on why I cant capture this?
|
It's not clear what you mean here. What are you attempting to query? Or, to put it another way, what results are you attempting to obtain?
|
|

January 26th, 2009, 10:35 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
|
|
Sorry
It was a bit late when I was posting. I have user data in a sql db tied to the current user that I wanted to display. I would like to be able to query that data to return the record only based on the current user logged in.
|
|

January 26th, 2009, 11:05 AM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
I'm not sure where the problem is. Can't you just write a query with the user name as a parameter?
|
|

January 26th, 2009, 01:55 PM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Imar
Thanks for your swift response. I understand what you are saying with
[string userName = Membership.GetUser().UserName;]
but I am afraid I still don't know where to put this.
I can get a page to allow a user to log in. And I can display the name of the user using the [LoginName]. I can use a datagrid to display the names of all the people in my database, but I cannot make the link between the logged in user to display only the information relevant to the logged in user in the datagrid.
I am sorry if I am being really stupid about this (I suspect I am!) but I apreciate your patience with a novice!
|
|

January 26th, 2009, 04:23 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It all depends on where you get your data from and in what way.
If you're using a SqlDataSource you need to create a parameter for the user and fill it in its Selecting event. If you're using ADO.NET code, you can pass it as a parameter on a Command object. If you're using an ObjectDataSource, you can get the name from the context in your code directly. I could go on for a while with all the other data sources that exist.
So, show me the code, tell me what you're doing and how, and I can recommend something a bit more concrete....
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

January 27th, 2009, 02:17 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
|
|
Heres my issue
Here is where I am storing the User:
<asp:LoginNameID="LoginName1"runat="server"CssClass="style14"/>
This lets me see the user logged in, but....
Here is what I am trying to query:
<asp:SqlDataSourceID="cookie"runat="server"
ConnectionString="<%$ ConnectionStrings:ProdConnectionString %>"SelectCommand="SELECT [UserName], [UserID], [LastName] FROM [TblUsers] WHERE ([UserName] LIKE '%' + @UserName + '%')">
<SelectParameters>
<asp:CookieParameterCookieName="'LoginName1'"Name="UserName"Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
This query does not return any values......any suggestions?
|
|
 |