扩展另一个 class 以创建自定义 class 类型 HttpContext.User
Extend another class to make custom class type HttpContext.User
我正在 Visual Studio 2013 年制作一个模拟社交媒体应用程序,使用 c#、asp.net-mvc5 和 entity framework 6. 我正在尝试添加自定义用户身份验证
我已经在 web.config:
中实现了身份验证
<authentication mode="Forms">
<forms loginUrl="~/AppUsers/Login" timeout="3000" />
</authentication>
并确保 ActionResult 在登录时创建一个 cookie
[HttpPost]
public ActionResult Login(string userName, string password)
{
AppUser user = null;
if (db.AppUser.Any(m => m.UserName == userName)) {
user = db.AppUser.Where(m => m.UserName == userName).First();
if (user.Password == password) {
FormsAuthentication.SetAuthCookie(userName, true);
return RedirectToAction("AppUserProfile", new { id = user.Id });
}
}
return RedirectToAction("Login", new { message = "Invalid Password" });
}
但我现在卡住了,每当我尝试检查是否 HttpContext.User.Identity.IsAuthenticated 时,我都会收到一条错误消息:
"An object reference is required for the non-static field, method, or property 'System.Web.HttpContext.User.get'"
我需要让我的 AppUser class 扩展一个 HttpContext class 来消除这个错误,还是我必须按原样重写整个 class?
public class AppUser
{
[Key]
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match")]
public string ConfirmPassword { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string EmailAddress { get; set; }
public virtual ICollection<UserPost> UserPosts { get; set; }
}
制作中
HttpContext.User.Identity.IsAuthenticated
进入
HttpContext.Current.User.Identity.IsAuthenticated
为我解决了问题
我正在 Visual Studio 2013 年制作一个模拟社交媒体应用程序,使用 c#、asp.net-mvc5 和 entity framework 6. 我正在尝试添加自定义用户身份验证 我已经在 web.config:
中实现了身份验证<authentication mode="Forms">
<forms loginUrl="~/AppUsers/Login" timeout="3000" />
</authentication>
并确保 ActionResult 在登录时创建一个 cookie
[HttpPost]
public ActionResult Login(string userName, string password)
{
AppUser user = null;
if (db.AppUser.Any(m => m.UserName == userName)) {
user = db.AppUser.Where(m => m.UserName == userName).First();
if (user.Password == password) {
FormsAuthentication.SetAuthCookie(userName, true);
return RedirectToAction("AppUserProfile", new { id = user.Id });
}
}
return RedirectToAction("Login", new { message = "Invalid Password" });
}
但我现在卡住了,每当我尝试检查是否 HttpContext.User.Identity.IsAuthenticated 时,我都会收到一条错误消息:
"An object reference is required for the non-static field, method, or property 'System.Web.HttpContext.User.get'"
我需要让我的 AppUser class 扩展一个 HttpContext class 来消除这个错误,还是我必须按原样重写整个 class?
public class AppUser
{
[Key]
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match")]
public string ConfirmPassword { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string EmailAddress { get; set; }
public virtual ICollection<UserPost> UserPosts { get; set; }
}
制作中
HttpContext.User.Identity.IsAuthenticated
进入
HttpContext.Current.User.Identity.IsAuthenticated
为我解决了问题