gpt4 book ai didi

c# - 创建用户时出现 ObjectDisposedException

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:31 27 4
gpt4 key购买 nike

我在尝试创建用户时遇到错误。我在下面的代码中的 第 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);
}
});

我进入了这个函数,我得到了正确的名字。

最佳答案

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,我可以登录:)

关于c# - 创建用户时出现 ObjectDisposedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000929/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com