aspnet Identity中在哪里配置applicationUserManager和角色管理器
where to configure applicationUserManager and role manager in aspnet Identity
首先,我使用了 AspNet Identity Sample - 通过包管理器控制台预先安装,但 razor 文件不匹配,因此 intellisense 停止在视图页面上工作。
所以,我选择逐步手动放置所有代码文件,并在它们出现时解决丢失问题。
所以,我的 App_Start 文件夹中有一个 identityConfig.cs 文件,如下所示----
public class IdentityConfig
{
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
在我的帐户控制器中,我有这样的代码--------
public class AccountController : Controller
{
public AccountController()
{
}
public AccountController(IdentityConfig.ApplicationUserManager userManager, IdentityConfig.ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
private IdentityConfig.ApplicationUserManager _userManager;
public IdentityConfig.ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<IdentityConfig.ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
现在,我在注册操作方法中遇到错误------
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email
};
var result = await UserManager.CreateAsync(user, model.Password);
// await UserManager.SetTwoFactorEnabledAsync(user.Id, true);
if (result.Succeeded)
{
return await GenerateEmailConfirmation(user);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
在这里,如您所见,在第六行,它在第 --------
行给出 System.NullReferenceException
var result = await UserManager.CreateAsync(user, model.Password);
我已经尝试调试它,在我的本地 window,它正在传递正确填充数据库所需的所有内容,但只有这个结果变量是 null.also,
_signInManager、userManager 和 SignInManager 以及 USerManager 在本地 window.
中都显示为空
我缺少的 problem.What 是什么。请告诉我。这个 UserMAnager class.I 不知道有什么关系。
确保在启动时创建了它们 class:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
...
}
}
首先,我使用了 AspNet Identity Sample - 通过包管理器控制台预先安装,但 razor 文件不匹配,因此 intellisense 停止在视图页面上工作。
所以,我选择逐步手动放置所有代码文件,并在它们出现时解决丢失问题。 所以,我的 App_Start 文件夹中有一个 identityConfig.cs 文件,如下所示----
public class IdentityConfig
{
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
在我的帐户控制器中,我有这样的代码--------
public class AccountController : Controller
{
public AccountController()
{
}
public AccountController(IdentityConfig.ApplicationUserManager userManager, IdentityConfig.ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
private IdentityConfig.ApplicationUserManager _userManager;
public IdentityConfig.ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<IdentityConfig.ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
现在,我在注册操作方法中遇到错误------
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email
};
var result = await UserManager.CreateAsync(user, model.Password);
// await UserManager.SetTwoFactorEnabledAsync(user.Id, true);
if (result.Succeeded)
{
return await GenerateEmailConfirmation(user);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
在这里,如您所见,在第六行,它在第 --------
行给出 System.NullReferenceException var result = await UserManager.CreateAsync(user, model.Password);
我已经尝试调试它,在我的本地 window,它正在传递正确填充数据库所需的所有内容,但只有这个结果变量是 null.also, _signInManager、userManager 和 SignInManager 以及 USerManager 在本地 window.
中都显示为空我缺少的 problem.What 是什么。请告诉我。这个 UserMAnager class.I 不知道有什么关系。
确保在启动时创建了它们 class:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
...
}
}