title: ASP.Net MVC 4 Generic Principal Difficulties
body: <p>I am developing an <strong>ASP.Net MVC 4</strong> web application. Previously my MVC applications have been developed using <strong>MVC 3</strong> and with this new <strong>MVC 4</strong> application I have just copied/ reused my <strong>authentication and authorisation code</strong> from previous applications.</p>
  
  <p>When a user logs into my site I do the following</p>
  
  <p><strong>Account Controller</strong></p>
  
  <pre><code>public ActionResult Login(LoginModel model, string returnUrl)
  {
      if (ModelState.IsValid)
      {
          User user = _userService.GetUser(model.Email.Trim());
  
          //Create Pipe Delimited string to store UserID and Role(s)
          var userData = user.ApplicantID.ToString();
  
          foreach (var role in user.UserRoles)
          {
              userData = userData + "|" + role.description;
          }
  
          _formAuthService.SignIn(user.ApplicantFName, false, userData);
  
          return RedirectToAction("Index", "Portfolio");
          }
  
          return View(model);
      }
  </code></pre>
  
  <p><strong>FormsAuthenticationService</strong></p>
  
  <pre><code>public class FormsAuthenticationService : IFormsAuthenticationService
  {
      public void SignIn(string userName, bool createPersistentCookie, string UserData)
      {
          if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
  
          // Create and tuck away the cookie
          FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(15), createPersistentCookie, UserData);
          // Encrypt the ticket.
          string encTicket = FormsAuthentication.Encrypt(authTicket);
  
          //// Create the cookie.
          HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
          HttpContext.Current.Response.Cookies.Add(faCookie);
      }
  }
  </code></pre>
  
  <p><strong>Global.asax</strong></p>
  
  <pre><code>protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {
  
      // Get the authentication cookie
      string cookieName = FormsAuthentication.FormsCookieName;
      HttpCookie authCookie = Context.Request.Cookies[cookieName];
  
      // If the cookie can't be found, don't issue the ticket
      if (authCookie == null) return;
  
      // Get the authentication ticket and rebuild the principal
      // &amp; identity
      FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
  
      string[] UserData = authTicket.UserData.Split(new Char[] { '|' });
  
      GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
      GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity, UserData);
      Context.User = userPrincipal;
  
  }
  </code></pre>
  
  <p>This code works well in my previous MVC 3 applications, but in this MVC 4 application, inside the Razor View, the following code does not seem to be accessing the <strong>IsInRole</strong> property to perform the role check</p>
  
  <pre><code>@if (HttpContext.Current.User.IsInRole("Applicant"))
  {
      &lt;p&gt;text&lt;/text&gt;
  }
  </code></pre>
  
  <p>Again, this worked perfectly in my MVC 3 applications.</p>
  
  <p>Does anyone have any ideas or suggestions as to why this won't work with my MVC 4 application?</p>
  
  <p>Any help is much appreciated.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Extra Info</strong></p>
  
  <p>My MVC 4 application is using .Net Framework 4.0</p>
  
  <p>The screenshot below shows my <strong>Generic Principal</strong> which is assigned to <strong>Context.User</strong>. You can see that for this User, the <strong>m_roles</strong> contains two strings, the <strong>UserID</strong> (100170) and their <strong>Role</strong>(Applicant). But for some reason, The <strong>IsInRoles</strong> cannot be accessed or seen in my <strong>MVC 4</strong> Razor View, however, it could in my identical <strong>MVC 3</strong> Razor View.
  <img src="https://i.stack.imgur.com/cC2U4.png" alt="enter image description here"></p>