返回 NULL 值的简单成员身份用户配置文件

本文关键字:身份 用户 配置文件 成员 简单 NULL 返回 | 更新日期: 2023-09-27 18:36:40

这个查询有什么问题,为什么它不为我返回一个用户对象。我可以验证用户是否已成功创建,我可以在数据库中看到记录,但我无法查询该对象,因此我可以将新创建的 UserId 用作另一个表中的 FK。

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new {AccountType = model.AccountType  }); 
                    WebSecurity.Login(model.UserName, model.Password);
                    var userProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
                    ?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
                    if (userProfile == null)
                    {
                        throw new Exception("no userProfile for this user");
                    }
                    if (userProfile.AccountType == AccountType.Retailer)
                    {
                        return RedirectToAction("Create", "Retailer");
                    }
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

这是帐户类型的枚举

public enum AccountType
    {
        Retailer = 1,
        Customer = 2,
        Manager = 3,
        Employee = 4
    }

谢谢,我正在寻找如何检索用户配置文件对象或登录用户的ID

返回 NULL 值的简单成员身份用户配置文件

与其使用

User.Identity.Name,不如尝试使用 model.UserName 进行查询