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

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

问题描述

我正在尝试掌握 ASP.NET MVC 5 中引入的新成员资格系统,但我遇到了一个小问题,我很确定您能帮我解决.

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.

我将基于 本教程 并为 ApplicationUser 引入了自定义属性,例如 Name、Surname、DOB 等.

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.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();

以上代码仅为示例.基本上,您需要探索 IdentityManageStore 属性.

Above code is an example only. Basically you need to explore the Store property of IdentityManage.

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

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