I would like to get a list of users in my application which uses the latest ASP.NET Identity so I created this controller which uses an Instance of UserManager and queries that with its own built in method (ASP.NET Identity 2)

namespace WebRole1.WebAPI
{
    public class UserProfileController : ApiController
    {

        public UserProfileController()
        {
        }
        public UserProfileController(ApplicationUserManager userManager)
        {
            UserManager = userManager;
        }

        private ApplicationUserManager _userManager;
        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }

        [HttpGet]
        [ActionName("GetMapData")]
        public HttpResponseMessage GetMapData()
        {
            var users = UserManager.Users
                .Select(u => new { Id = u.Id, Name = u.UserName })
                .ToList();
            return Request.CreateResponse(HttpStatusCode.OK, users);
        }

Is this any more efficient than if I was to create an Identity context and then use Entity Framework to go issue the same SQL against the database. For me it seems like it would be easier to do the latter as I would be more in control and the code would look simpler.

有帮助吗?

解决方案

Its not any more efficient, the basic premise is that if you ever wanted to replace the EF implementation with something else (a no sql backend for example), you wouldn't have to change much of your application code compared to directly using EF apis hanging off of the Identity context. That is typically the tradeoff that should be considered.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top