Why don't the UserManager of Identity recognize the Create method in configuration method?

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(() =&gt; new UsersPhonesDBContext());

    app.CreatePerOwinContext&lt;RoleManager&lt;AppRole&gt;&gt;((options, context) =&gt;
        new RoleManager&lt;AppRole&gt;(
            new RoleStore&lt;AppRole&gt;(context.Get&lt;UsersPhonesDBContext&gt;())));

    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&lt;AppUserManager&gt; options, IOwinContext context)
    {
        var manager = new AppUserManager(
            new UserStore&lt;AppUsers&gt;(context.Get&lt;UsersPhonesDBContext&gt;()));

        // 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&lt;Users&gt; PhoneUsers { get; set; }
    public DbSet&lt;Phones&gt; Phones { get; set; }
    public DbSet&lt;Sims&gt; Sims { get; set; }
}


#c-sharp #asp.net

5 Likes9.40 GEEK