Question

I am beyond confused regarding user authentication in asp.net MVC5... I've been referring this article:

http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

And this questions:

MVC 5 Custom UserStore ASP.net Identity - How does UserManager<TUser> Have Access To Roles?

...and getting absolutely know where. Here is the code in my IdentityModel.cs

namespace attempt_4.Models
{

public class ApplicationUser : IdentityUser
{
     public string FirstName { get; set; }
}

public class MyUserStore : IUserStore<ApplicationUser>
{
}

public class ApplicationDbContext<ApplicationUser> : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}
}

And in the controller I'm attempting to get the UserObject with:

   var userManager = new UserManager<ApplicationUser>(new MyUserStore<ApplicationUser>(new ApplicationDbContext()));
  var user = userManager.FindById(User.Identity.GetUserId());

This gives me a plethora of compile errors most in the form of attempt_4.Models.MyUserStore<ApplicationUser>' does not implement interface member 'System.IDisposable.Dispose(), but also one saying ApplicationUser cannot be used as Type parameter 'TUser'.

At the moment I'm using:

var id = User.Identity.GetUserId();
var user = db.Users.First(x => x.Id == id);

This can't be the best way, no doubt the db was already queried due to the [Authorize].

I don't need anything involving roles or anything fancy... just want to extend the User model and be able to reference it from other models to do stuff like this: db.Apartments.where(apt => apt.OwnerId == currentUserID). That said, what is the best practice for linking tables to the User table? Should I have a int id in the ApplicationUser class to query by? Or should I just use the Guid?

Was it helpful?

Solution

You're getting an error because you haven't implented the interface you've stated (IUserStore). It's not necessary to make your own UserStore either, you can just use the one that comes out of the box:

new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))

If this doesn't meet your requirements, here's a great guide on how to make your own implementation of ASP.NET Identity: http://odetocode.com/blogs/scott/archive/2014/01/20/implementing-asp-net-identity.aspx

Also, I believe that [Authorize]is just authorizing the user's cookie, not querying the database in any way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top