MVC5 ApplicationUser自定义属性 [英] MVC5 ApplicationUser custom properties

查看:392
本文介绍了MVC5 ApplicationUser自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图去与ASP.NET MVC 5引入的新会员系统夹具和我遇到一个小问题,对此我pretty相信你一定能帮助我。

I am trying to get to grips with the new Membership system introduced in ASP.NET MVC 5 and I've come across a small issue which I am pretty sure you will be able to help me with.

我要基于关闭<一个href=\"http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on\"相对=nofollow>本教程并相继推出,如姓名,出生日期等,自定义属性ApplicationUser

I am going based off this tutorial and have introduced custom properties to ApplicationUser such as Name, Surname, DOB, etc.

不过,而不是创建用户,我试图更新一个当前登录。我在看这是目前用来更改密码控制器的方法。

However, instead of creating the user, I am trying to update the currently logged in one. I am looking at the controller method which is currently used to change password.

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或?

How exactly would I go on about updating an ApplicationUser's Surname for example? Do I need to call the DbContext or?

我希望我的问题是清楚的。

I hope my question is clear.

推荐答案

浏览 IdentityManager.Store.UserManagement IdentityManager.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();

以上code是只是一个例子。基本上你需要探索的商店属性 IdentityManage

这篇关于MVC5 ApplicationUser自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆