Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems implementing IPrincipal
    primarykey
    data
    text
    <p>Trying to implement IPrincipal (ASP.NET MVC 3) and having problems: my custom IPrincipal:</p> <pre><code> interface IDealsPrincipal: IPrincipal { int UserId { get; set; } string Firstname { get; set; } string Lastname { get; set; } } public class DealsPrincipal : IDealsPrincipal { public IIdentity Identity { get; private set; } public bool IsInRole(string role) { return false; } public DealsPrincipal(string email) { this.Identity = new GenericIdentity(email); } public int UserId { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } </code></pre> <p>To serialize/deserialize i use the following class:</p> <pre><code>public class DealsPrincipalSerializeModel { public int UserId { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } </code></pre> <p>The Application authenticate event is as follows (works fine!)</p> <pre><code>protected void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { //get the forms ticket FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); //instantiate a new Deserializer JavaScriptSerializer serializer = new JavaScriptSerializer(); //deserialize the model DealsPrincipalSerializeModel serializeModel = serializer.Deserialize&lt;DealsPrincipalSerializeModel&gt;(authTicket.UserData); //put the values of the deserialized model into the HttpContext DealsPrincipal newUser = new DealsPrincipal(authTicket.Name); //this implements IPrincipal newUser.UserId = serializeModel.UserId; newUser.Firstname = serializeModel.Firstname; newUser.Lastname = serializeModel.Lastname; HttpContext.Current.User = newUser; } } </code></pre> <p>As you can see in the last statement the HttpContext gets assigned this new DealsPrincipal (which works fine).</p> <p>The problem is that if want to access this User in a Controller(Action) i always get a base class object. If i cast the User as follows:</p> <pre><code>User as DealsPrincipal </code></pre> <p>to get for example the UserId (sample: </p> <pre><code>( User as DealsPrincipal).UserId </code></pre> <p>this is always null!!! Why? What am i missing?</p>
    singulars
    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.
    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