2

ユーザーを作成しようとすると、エラーが発生します。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);
      }
});

私は関数に入り、正しい名前を持っています。

4

1 に答える 1

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 に追加され、ログインできます:)

于 2015-06-23T14:41:25.423 に答える