I want to ask Owin if a user exists. Putting together random stuff I've found online, I end up with this (totally untested code):
/// <summary> Returns true if the TestUser user exists in the membership database. </summary>
public static bool UserExists(ApplicationDbContext db, string username)
{
var userManager = new Microsoft.AspNet.Identity.UserManager<ApplicationUser>(
new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(db));
var user = userManager.FindByName(username);
return (user != null);
}
My question is, do I really need that long, ugly expression to get a new UserManager? Or is there a better, more efficient, cleaner, or more correct way to do this?
And now that I look a little more closely at it, I see that both UserManager and UserStore are disposable. Do I want to wrap things in using
blocks?
It's the same as creating most any object, but in this case I would add some using statements to simplify it:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
Which would make your instantiation statement much tidier:
var userManager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(db));