我在尝试创建用户时遇到错误。我在以下代码中的line 15
处获得了它。
1 public async void AddOrganisationAdmin()
2 {
3
4 String OrganisationName = Request["OrganisationName"];
5 Debug.Print(OrganisationName);
6 RegisterViewModel model = new RegisterViewModel();
7 model.Email = "admin@" + OrganisationName;
8 model.Organisation = OrganisationName;
9 model.Password = "adminPassword!1";
10 model.ConfirmPassword = "adminPassword!1";
11 model.Role = "Organisation Administrator";
12
13
14 var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Organisation = model.Organisation, Role = model.Role };
15 IdentityResult result = await UserManager.CreateAsync(user, model.Password);
16 }
错误是:
An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but
was not handled in user code
Additional information: Cannot access a disposed object.
处理什么物体?这些行是asp.net mvc 5项目中默认寄存器功能的副本,所以我做错了什么?
我从这样的ajax帖子中调用该函数:
$.ajax({
url: "/Account/AddOrganisationAdmin",
type: 'POST',
data: { OrganisationName : "@CompanyName"},
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr);
}
});
我进入该功能,我的名字正确。
答案 0 :(得分:0)
tacos_tacos_tacos的评论让我稍微调查了一下dbcontext,经过一段时间和错误后,我得到了以下内容
1 public void AddOrganisationAdmin()
2 {
3
4 String OrganisationName = Request["OrganisationName"];
5 Debug.Print(OrganisationName);
6 RegisterViewModel model = new RegisterViewModel();
7 model.Email = "admin@" + OrganisationName + ".com";
8 model.Organisation = OrganisationName;
9 model.Password = "adminPassword!1";
10 model.ConfirmPassword = "adminPassword!1";
11 model.Role = "Organisation Admin";
12 Promato.Models.ApplicationDbContext dbcontext = new Promato.Models.ApplicationDbContext();
13
14 var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Organisation = model.Organisation, Role = model.Role };
15 user.PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password);
16 user.SecurityStamp = Guid.NewGuid().ToString();
17 dbcontext.Users.Add(user);
18 dbcontext.SaveChanges();
19 }
这段代码对我有用。用户被添加到.mdf,我可以登录:)