Im trying to pass a users name in ASP.NET MVC 4 after they have logged in but I cant figure out a way to get the data to transfer.
Here is the Login Action named "Auth" that gets the users info after its set.
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");
}
After the name variable is set, i try to get the value from my PostController's Index View which is the index of my site using inheritance to display but the variable resets to null.. Heres the Index action used to call the name variable from my AccountController where the Auth Action resides.
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));
}
}
Is there another way I can do this? Im trying to achieve getting the users info after they login to display it on the index page along with their profile pic.. Please help!!