在我的 ASP.NET MVC 应用程序中,我正在尝试创建一个自定义 HttpContent.User 对象。我首先创建了一个实现 IPrincioal 的 Member 类。
public class Member : IPrincipal
{
public string Id { get; set; }
public IIdentity Identity { get; set; }
public bool IsInRole(string role) { throw new NotImplementedException(); }
...
}
然后在身份验证时,我将 HttpContext.User 设置为 Member 类的实例:
FormsAuthentication.SetAuthCookie(email, false);
HttpContext.User = member;
然后稍后我想检查用户是否经过身份验证,如下所示:
if (User.Identity.IsAuthenticated) { ... }
这就是我卡住的地方。我不确定我需要为public IIdentity Identity
成员实例上的属性做什么。这样我就可以使用 HttpContext.User 对象,如下所示:
IsAuthenticated = HttpContext.User.Identity.IsAuthenticated;
ViewBag.IsAuthenticated = IsAuthenticated;
if (IsAuthenticated) {
CurrentMember = (Member)HttpContext.User;
ViewBag.CurrentMember = CurrentMember;
}