所以我只希望允许登录用户自己预订假期。我认为最简单的方法是将登录用户的“名称”与人员表中的“名称”进行比较。所以....
public ActionResult Create()
{
string xx = (string)Session["usernameID"];
int? currentPersonID = Convert.ToInt32(xx);
string userNameComparedAgainstLoginName = // here is where i want to say 'name' of logged in user
CreateModel model = new CreateModel();
model.currentPersonID = currentPersonID.Value;
model.PList4DD = db.People.ToList();
if (userNameComparedAgainstLoginName == model.userName)
{
ViewBag.Id = new SelectList(db.People, "Id", "Name");
return View(model);
}
else
{
TempData["canOnlyBookHolidaysForYourself"] = "I'm afraid you can only book holidays for yourself";
return RedirectToAction("Index");
}
}
用户注册时给出的名称将与数据库中使用的名称相同。
那么有人能告诉我如何访问登录的“名称”吗?
谢谢
最佳答案
将记录的用户数据(例如用户名/电子邮件/ID)存储在 session 中是不好的做法。 好的做法是创建自定义用户主体或身份,并将应用程序中经常使用的所有用户数据存储在其中。
您最好创建自定义主体或身份并将该信息存储在身份验证 cookie 中并通过 User.Identity.XXX 访问它
这是一个链接,其中很好地描述了如何创建自定义主体。 ASP.NET MVC - Set custom IIdentity or IPrincipal
或者,如果您不需要如此复杂的机制,您可以只使用 HttpContext 中的默认 User.Identity。
祝你好运!
关于c# - 如何访问用户注册时给出的 'name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13949644/