2013-10-13 11 views
5

私は、ASP.NET MVC 5で導入された新しいメンバシップシステムを手に入れようとしています。私はあなたが私を助けてくれると確信している小さな問題に出くわしました。MVC5 ApplicationUserカスタムプロパティ

私はthis tutorialオフに基づいつもりですし、などの名前、姓、DOB、とApplicationUserに

をカスタムプロパティを導入しているが、代わりにユーザーを作成するので、私は現在、1ログインを更新しようとしています。私は現在、パスワードを変更するために使用されているコントローラメソッドを見ています。

public async Task<ActionResult> Manage(ManageUserViewModel model) 
     { 
      string userId = User.Identity.GetUserId(); 
      bool hasLocalLogin = await IdentityManager.Logins.HasLocalLoginAsync(userId); 
      ViewBag.HasLocalPassword = hasLocalLogin; 
      ViewBag.ReturnUrl = Url.Action("Manage"); 

      if (hasLocalLogin) 
      {    
       if (ModelState.IsValid) 
       { 
        IdentityResult result = await IdentityManager.Passwords.ChangePasswordAsync(User.Identity.GetUserName(), model.OldPassword, model.NewPassword); 
        if (result.Success) 
        { 
         return RedirectToAction("Manage", new { Message = "Your password has been changed." }); 
        } 
        else 
        { 
         AddErrors(result); 
        } 
       } 
      } 
      else 
      { 
       // User does not have a local password so remove any validation errors caused by a missing OldPassword field 
       ModelState state = ModelState["OldPassword"]; 
       if (state != null) 
       { 
        state.Errors.Clear(); 
       } 

       if (ModelState.IsValid) 
       { 
        // Create the local login info and link it to the user 
        IdentityResult result = await IdentityManager.Logins.AddLocalLoginAsync(userId, User.Identity.GetUserName(), model.NewPassword); 
        if (result.Success) 
        { 
         return RedirectToAction("Manage", new { Message = "Your password has been set." }); 
        } 
        else 
        { 
         AddErrors(result); 
        } 

      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

たとえば、ApplicationUserの姓をどのように更新すればよいでしょうか? DbContextまたは?を呼び出す必要がありますか?

私の質問がはっきりしていることを願っています。

答えて

6

IdentityManager.Store.UserManagementIdentityManager.Store.Usersを検索してください。

ApplicationUser cUser = (ApplicationUser) await IdentityManager.Store.Users.FindByNameAsync(HttpContext.User.Identity.Name, new System.Threading.CancellationToken()); 
cUser.Surname = "New Something"; 
IdentityResult result1 = await IdentityManager.Store.SaveChangesAsync(); 

上記のコードは一例です。基本的には、プロパティをIdentityManageに設定する必要があります。

+0

+1 BTWを1つ作成するのではなく、System.ThreadingでCancellationToken.Noneを使用できます。 – Stimul8d

+0

すごいスタッフたち!ありがとう – teh0wner

2

データベースコンテキストのUsersオブジェクトを使用したとき、他のトラッキングエラーが発生しました。これは

UserManager.Update(user); 

user.StorageName = "gooblygook"; 
//whatever other properties you would like to use 

そして、我々はコントローラでのUserManagerとそれを救う:私たちのアプリケーションでは、我々は、このような

var user = UserManager.FindById(userId); 

編集としてユーザーにプロパティを取得します現在私たちのために働く解決策です。

2

Applicationオブジェクトの定義でPersonオブジェクトを仮想とマークします。それは私のために働いた。

public class ApplicationUser : IdentityUser 
{ 
    public virtual Person Person { get; set; } 
関連する問題