p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Visual Web Developer 2008 (http://p2p.wrox.com/forumdisplay.php?f=353)
-   -   Displaying data based on user logged in (http://p2p.wrox.com/showthread.php?t=72404)

Spider January 25th, 2009 10:20 AM

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.

Imar January 25th, 2009 10:35 AM

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

Lee Dumond January 25th, 2009 01:02 PM

Quote:

Originally Posted by Spider (Post 233663)
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.

mashype January 26th, 2009 06:25 AM

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?

Lee Dumond January 26th, 2009 10:02 AM

Quote:

Originally Posted by mashype (Post 233698)
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?

mashype January 26th, 2009 10:35 AM

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.

Lee Dumond January 26th, 2009 11:05 AM

I'm not sure where the problem is. Can't you just write a query with the user name as a parameter?

Spider January 26th, 2009 01:55 PM

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!

Imar January 26th, 2009 04:23 PM

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

mashype January 27th, 2009 02:17 AM

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?


All times are GMT -4. The time now is 04:00 AM.

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