我试图在他们登录后在 ASP.NET MVC 4 中传递用户名,但我想不出一种方法来传输数据。
这是名为“Auth”的登录操作,它在设置后获取用户信息。
public string name;
account userInfo;
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Auth(account user)
{
if (ModelState.IsValid)
{
userInfo = GetUser(user);
if (userInfo.rank == "a")
{
Session["IsAdmin"] = true;
Session["IsMember"] = true;
name = userInfo.username;
return RedirectToAction("Index", "Mod");
}
if (userInfo.rank == "member")
{
Session["IsMember"] = true;
name = userInfo.username;
return RedirectToAction("Index", "Post");
}
}
name = userInfo.username;
return View("Login");
}
设置名称变量后,我尝试从我的 PostController 的索引视图中获取值,这是我的站点的索引,使用继承来显示,但变量重置为空。这是用于从我的 AccountController 调用名称变量的索引操作Auth Action 所在的位置。
public class PostController : AccountController
{
public ActionResult Index(int? id)
{
/*EncryptConString();*/
int pageNum = id ?? 0;
IEnumerable<Post> posts =
(from post in db.Posts
where post.time < DateTime.Now && post.category == "Blog Post"
orderby post.time descending
select post).Skip(pageNum * postCount).Take(postCount +1);
ViewBag.IsPreviousLinkVisible = pageNum > 0;
ViewBag.IsNextLinkVisible = posts.Count() > postCount;
ViewBag.PageNumber = pageNum;
Session["ShowSide"] = true;
ViewBag.showSidebar = showSidePanel;
ViewBag.SecAbvPosts = secAbvPosts;
ViewBag.IsAdmin = IsAdmin;
ViewBag.IsMember = IsMember;
ViewBag.Name = name;
return View(posts.Take(postCount));
}
}
还有其他方法可以做到这一点吗?我试图在用户登录后获取用户信息以将其与他们的个人资料图片一起显示在索引页面上。请帮助!