C# 如何在其他页面中使用我的登录用户
我不熟悉使用C#MVC进行后端编程,我创建了一个有效的登录系统C# 如何在其他页面中使用我的登录用户,c#,asp.net-mvc,C#,Asp.net Mvc,我不熟悉使用C#MVC进行后端编程,我创建了一个有效的登录系统 public ActionResult Login(LogingInViewModel Login) { if (ModelState.IsValid) { User user = _context.users.FirstOrDefault(u => u.Email == Login.Email); if (user != null) {
public ActionResult Login(LogingInViewModel Login)
{
if (ModelState.IsValid)
{
User user = _context.users.FirstOrDefault(u => u.Email == Login.Email);
if (user != null)
{
if (user.Password== Crypto.SHA256(Login.Password))
{
user.Token = Guid.NewGuid().ToString();
_context.SaveChanges();
HttpCookie tokenCookie = new HttpCookie("token")
{
Value=user.Token,
HttpOnly=true
};
tokenCookie.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(tokenCookie);
return RedirectToAction("index","Home",user);
}
}
ModelState.AddModelError("CustomError", "Wrong Email or Password");
}
LoginViewModel model1 = new LoginViewModel
{
Login = Login
};
return View("~/Views/Login/Index.cshtml", model1);
}
但我不知道如何在我的所有其他页面/控制器甚至共享/布局中使用该对象来显示用户名和配置文件图片。
我在网上查了一下,尝试了一些解决方案,但效果并不理想
@if (Model != null) { <span>@Model.Fullname</span>}
else { <span>Log in</span> }
@if(Model!=null){@Model.Fullname}
else{登录}
当然,因为我不能发送对象,所以它是空的。您可以使用包含任何信息的会话,也许您不了解声明和标识。您可以使用
HttpContext.CurrentUser.Username
?