为什么Identity的UserManager不识别配置方法中的Create方法?
Why don't the UserManager of Identity recognize the Create method in configuration method?
我是 ASP.NET Identity 的新手,创建了一个简单的登录过程,并在我项目的 IdentityConfig
配置方法中注册了委托。
我正在尝试注册它们,但 UserManager
和 RoleManager
class 无法识别 Create
方法。
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"),
});
}
}
登录方式:
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();
}
更新:
我在里面写了 class AppUserManager 和方法:
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;
}
}
还是
var manager = new AppUserManager(
new UserStore<AppUsers>(context.Get<UsersPhonesDBContext>()
抛出错误。
值不能为空。
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; }
}
我不确定您从哪里获得该代码,但是 UserManager<T>
或 RoleManager<T>
上没有名为 Create
的静态方法。根据 some tutorials 你应该自己写那个方法:
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
return manager;
}
如您所见,它只是一种创建正确 UserManager<T>
类型的方法。在本例中,它是一个名为 ApplicationUserManager
.
的自定义用户管理器
This SO answer 也提到了相同代码的部分内容。
我是 ASP.NET Identity 的新手,创建了一个简单的登录过程,并在我项目的 IdentityConfig
配置方法中注册了委托。
我正在尝试注册它们,但 UserManager
和 RoleManager
class 无法识别 Create
方法。
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"),
});
}
}
登录方式:
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();
}
更新:
我在里面写了 class AppUserManager 和方法:
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;
}
}
还是
var manager = new AppUserManager(
new UserStore<AppUsers>(context.Get<UsersPhonesDBContext>()
抛出错误。
值不能为空。
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; }
}
我不确定您从哪里获得该代码,但是 UserManager<T>
或 RoleManager<T>
上没有名为 Create
的静态方法。根据 some tutorials 你应该自己写那个方法:
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
return manager;
}
如您所见,它只是一种创建正确 UserManager<T>
类型的方法。在本例中,它是一个名为 ApplicationUserManager
.
This SO answer 也提到了相同代码的部分内容。