-1

I'm currently learning asp.net using web forms. Currently I am working on a basic profile system for a logged in user than can directly edit the information the user registered with.

Upon doing some research I decided to use the identity feature built right in to the standard web forms application. I spent a lot of time trying to figure out the best way to create a CRUD type of way to edit the identity information and finally decided on using the Scaffold function. (I'm still new to this - please let me know if there is a better way to do this)

Anyways, I've successfully created a CRUD for the identity. NOW I just would like to be able to ONLY show the data from the current logged in user.

I have tried playing with the httpcontext.current.user.identity or the getUserName() feature, I've played a bit to sql statements as well. I've tried looking on google and can't find exactly what I need.

Can anyone help with this please?

Note: I am using IDENTITY and not MEMBERSHIP. I am also using Web Forms and not MVC.

Here is the code on github.

https://github.com/madelinelise/Project

1 Answers1

0

The short answer, based on the username you can retrieve all the applicable user details for the user from the database.

You may use something similar to this:

var currentUser = DbCtx.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);

Edit:

var currentUser = DbCtx.Users.FirstOrDefault(u => u.UserName == User.Identity.Name).Select(u => new MyAppUser
{
    Id = u.Id,
    UserName = u.UserName // etc...
});
user1477388
  • 20,790
  • 32
  • 144
  • 264
  • Thank you! You got me one step closer. Now I'm getting an error: Error 1 Cannot implicitly convert type 'WebApplication2.Models.MyAppUser' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) I've been trying to research this error on google and can't really find what I need.. – Madeline Schimenti Mar 29 '15 at 18:50
  • What line of code gives you the error? The line I gave you just returns the the entity. If you have created a custom class implementing `IdentityUser` then you probably added your new properties directly on the database object. In which case, all you would need would be the entity your a returning, no need to cast it to another type. If you want to cast it, I've updated my code with an example. – user1477388 Mar 30 '15 at 14:23