im试图将userId
存储在AspNetUsers
表中,并在注册过程中将其存储在我的Student
表中。
如何从AspnetUsers
表中获取新创建的用户的userId
(不是登录的用户),然后将其存储在期间的单独表中?
我的控制器:
public ActionResult Create(ExpandedUserDTO paramExpandedUserDTO)
{
try
{
if (paramExpandedUserDTO == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var Email = paramExpandedUserDTO.Email.Trim();
var UserName = paramExpandedUserDTO.Email.Trim();
var Password = paramExpandedUserDTO.Password.Trim();
UserName = Email.ToLower();
// Create user
var objNewAdminUser = new ApplicationUser { UserName = UserName, Email = Email};
var AdminUserCreateResult = UserManager.Create(objNewAdminUser, Password);
if (AdminUserCreateResult.Succeeded == true)
{
string strNewRole = Convert.ToString(Request.Form["Roles"]);
if (strNewRole != "0")
{
// Put user in role
UserManager.AddToRole(objNewAdminUser.Id, strNewRole);
//Put user in student table
if (strNewRole == "Student")
{
}
}
return Redirect("~/Admin");
}
}
catch (Exception ex)
{
ViewBag.Roles = GetAllRolesAsSelectList();
return View("Create");
}
}
如果创建用户为您创建ID。
var objNewAdminUser = new ApplicationUser { UserName = UserName, Email = Email};
var userId = objNewAdminUser.Id //18479b0a-17cd-4a76-9e9e-d90a8480617b
you can try this
if(AdminUserCreateResult.Succeeded == true)
{
//Get Details of currently registered User
var RegisteredUser= SignInManager.UserManager.FindByEmail(Email);
//Extract userid from user Details.
var userid=RegisteredUser.Id;
//this will give you the ID stored in aspnetusers of currentUser
}