Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC5 custom principal not IsInRole not working
    primarykey
    data
    text
    <p>Since I upgraded my project to MVC5 in VS2012 the Custom principal I implemented isn't working anymore.</p> <p>The principal:</p> <pre><code>public class CustomPrincipal : IPrincipal { public Boolean IsInRole(String role) { return ((CustomIdentity)Identity).User.Roles.Any(r =&gt; r.Name == role); } public IIdentity Identity { get; private set; } public CustomPrincipal(UserSession userSession) { Identity = new CustomIdentity(userSession); } } </code></pre> <p>Custom identity:</p> <pre><code>public class CustomIdentity : IIdentity { public String Name { get; private set; } public String AuthenticationType { get { return "CustomAuthentication"; } } public Boolean IsAuthenticated { get; private set; } public User User { get; private set; } public UserSession UserSession { get; private set; } public CustomIdentity(UserSession userSession) { User = userSession.User; UserSession = userSession; Name = userSession.User.UserName; IsAuthenticated = userSession.User.IsApproved &amp;&amp; !userSession.User.IsDisabled &amp;&amp; !userSession.User.IsLockedOut; } } </code></pre> <p>How I set the User:</p> <pre><code>var principal = new CustomPrincipal(userSession); HttpContext.Current.User = principal; </code></pre> <p>The <code>[Authorize(Roles = "Administrator")]</code> attribute doesn't work anymore, neither does <code>var a = HttpContext.User.IsInRole("Administrator");</code></p> <p>When I place a breakpoint at <code>IsInRole</code> it never reaches there, but the constructed is called and <code>HttpContext.Current.User</code> does contain the <code>CustomPrincipal</code> object.</p> <p>What could be changed or wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload