I am new to ASP.NET Identity and created a simple sign-in process and have registered the delegates in configuration method of IdentityConfig
of my project.
I am trying to register them but the UserManager
and RoleManager
classes are not recognizing the Create
method.
public class IdentityConfig
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext<UserManager<AppUsers>>(UserManager<AppUsers>.Create);
app.CreatePerOwinContext<RoleManager<AppRole>>(RoleManager<AppRole>.Create);
app.CreatePerOwinContext(() => new UsersPhonesDBContext());
app.CreatePerOwinContext<RoleManager<AppRole>>((options, context) =>
new RoleManager<AppRole>(
new RoleStore<AppRole>(context.Get<UsersPhonesDBContext>())));
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Home/Login"),
});
}
}
Login Method:
public ActionResult Login()
{
var userManager = HttpContext.GetOwinContext().GetUserManager<UserManager<AppUsers>>();
var roleManager = HttpContext.GetOwinContext().GetUserManager<RoleManager<AppRole>>();
var authManager = HttpContext.GetOwinContext().Authentication;
AppUsers user = userManager.FindByName("MyName");
if (user != null)
{
var ident = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
//use the instance that has been created.
authManager.SignIn(
new AuthenticationProperties { IsPersistent = false }, ident);
return Redirect(Url.Action("Index", "Rest"));
}
// AppUsers user= userManager.Find("Hunain","");
return View();
}
Update:
I wrote the class AppUserManager and method inside it:
public class AppUserManager: UserManager<AppUsers>
{
public AppUserManager(IUserStore<AppUsers> store): base(store)
{
}
// this method is called by Owin therefore best place to configure your User Manager
public static AppUserManager Create(
IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
var manager = new AppUserManager(
new UserStore<AppUsers>(context.Get<UsersPhonesDBContext>()));
// optionally configure your manager
// ...
return manager;
}
}
Still
var manager = new AppUserManager(
new UserStore<AppUsers>(context.Get<UsersPhonesDBContext>()
throws error.
Value cannot be null.
My DB context class:
public class UsersPhonesDBContext: IdentityDbContext<AppUsers>
{
public UsersPhonesDBContext()
: base("UsersPhonesDBContext")
{
Database.SetInitializer<UsersPhonesDBContext>(null);
}
public DbSet<Users> PhoneUsers { get; set; }
public DbSet<Phones> Phones { get; set; }
public DbSet<Sims> Sims { get; set; }
}
I'm not sure where you got that code from, but there's no static method called Create
on either UserManager<T>
or RoleManager<T>
. According to some tutorials you're supposed to write that method yourself:
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
return manager;
}
As you can see, it's just a method to create the proper UserManager<T>
type. In this case, it's a custom user manager called ApplicationUserManager
.
This SO answer also mentions parts of the same code.