In my ASP.NET MVC application, I am trying to create a custom HttpContent.User object. I started by creating a Member class that implements IPrincioal.
public class Member : IPrincipal { public string Id { get; set; } public IIdentity Identity { get; set; } public bool IsInRole(string role) { throw new NotImplementedException(); } ... }
Then, during authentication, I set HttpContext.User to an instance of the Member class:
FormsAuthentication.SetAuthCookie(email, false); HttpContext.User = member;
Then later I want to check if the user is authenticated, for example:
if (User.Identity.IsAuthenticated) { ... }
Where I am stuck. I'm not sure what I need to do for the public IIdentity Identity in the member instance. . So I can use the HttpContext.User object something like this:
IsAuthenticated = HttpContext.User.Identity.IsAuthenticated; ViewBag.IsAuthenticated = IsAuthenticated; if (IsAuthenticated) { CurrentMember = (Member)HttpContext.User; ViewBag.CurrentMember = CurrentMember; }
source share