0

Situation: IdentityUser is inherited by AppUser.

AppUser has a few properties more.((int)ExClientNr and (string)Category)

On top

UserManager <IdentityUser> userManager

request

var user = await userManager.FindByEmailAsync(Input.Email);

When I hover over user (of type IdentityUser) in runtime, I see all AppUser properties with values in the list.

But when I type

var exClientNr = User.ExClientNr; 

the field ExClientNr or Category is not recognized in the intellisense.All fields of IdentityUser are of cause.

User.Getype() gives me {Name="AppUser" FullName="namespace.Models.AppUser"} so it is even aware of his type.

Question: Can someone tell me how to get the value from those extra properties from this object.

Dinand
  • 330
  • 2
  • 17
  • Why `UserManager userManager` and not `UserManager userManager`? – TheBatman Feb 25 '20 at 15:56
  • An object has a type identity. Variables are of a particular type. So you can write `Base baseObj = new Sub();` (assuming `Sub` inherits from `Base`). As far as the compiler knows, the `baseObj` variable is a reference to an object of type `Base`. The object itself is of type `Sub` (which makes it a `Base` as well). If you are passing around `Base`-typed variables, but you need access to the `Sub`-ishness of `Sub`-typed objects, you can use `if (baseObj is Sub subObj) {subObj.SubCall();}`. That, however, is usually a code-smell – Flydog57 Feb 25 '20 at 16:17

1 Answers1

1

You can use generic UserManager with the right type:

UserManager <AppUser> userManager

Or cast your user in run-time:

var user = await userManager.FindByEmailAsync(Input.Email);
var exClientNr = (user as AppUser)?.ExClientNr; 
Anton
  • 801
  • 4
  • 10
  • Anton Thank you. With the first option I'm not able to use. Just after this code I login with the signin manager who needs a IdentityUser as param. The second option gives a convertion error. – Dinand Feb 26 '20 at 07:28
  • You can use `SignInManager`. Moreover, if your `AppUser` is inherited from `IdentityUser`, you can pass it event in SignManager with IdentityUser type – Anton Feb 26 '20 at 08:34
  • And that's where the error comes, how strange it sounds, but it does not accept the appuser for the signin. I solved it now by 2 requests to the DB. Not nice but it works. Thank you for trying . – Dinand Feb 27 '20 at 09:35
  • It is not a good solution. How did you setup Identity in Startup.cs and could you provide AppUser code? – Anton Feb 27 '20 at 11:18