我已经使用ASP.NET Identity 2来创建注册用户逻辑,我在HttpContext.GetOwinContext()上有一个例外.GetUserManager()。
可能有什么问题?谢谢你的任何建议。
有注册控制器
public class AccountController : Controller
{
private ApplicationUserManager UserManager
{
get
{
return HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
public ActionResult Register()
{
return View();
}
[HttpPost]
public async Task<ActionResult> Register(RegisterUserModel model)
{
if (ModelState.IsValid)
{
AppUser user = new AppUser { UserName = model.Email, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
return RedirectToAction("Login", "Account");
}
else
{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
}
return View(model);
}
}
ApplicationUserManager逻辑
public class ApplicationUserManager : UserManager<AppUser>
{
public ApplicationUserManager(IUserStore<AppUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
IdentificationContext db = context.Get<IdentificationContext>();
ApplicationUserManager manager = new ApplicationUserManager(new UserStore<AppUser>(db));
return manager;
}
}
注册模式
public class RegisterUserModel
{
[Required]
[StringLength(255)]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(255, MinimumLength = 5)]
public string Password { get; set; }
[Required]
[System.ComponentModel.DataAnnotations.Compare("Password")]
[DataType(DataType.Password)]
public string PasswordConfirm { get; set; }
}
继承自IdentitiyUSer模型的模型
public class AppUser : IdentityUser
{
public AppUser() { }
}
启动课程
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext<IdentificationContext>(IdentificationContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
});
}
有一个异常输出
答案 0 :(得分:0)
您需要在web.config
中添加设置(owin:AutomaticAppStartup)<add key="owin:AutomaticAppStartup" value="true"/>
如果仍然无效,请添加以下设置,指定您的启动类。这是因为Startup类的默认约定是[AssemblyName] .Startup。如果您不再遵循该约定,则需要在Web.Config中指定Startup类的全名。
<add key="owin:AppStartup" value="[Namespace].Startup, [AssemblyName]" />
希望它有所帮助。